← Quiz: Python Dictionaries: all 15 questions
Quiz: Python Dictionaries · Question 12 of 15
What does d["z"] do if the key "z" is not in the dictionary d?
What does d["z"] do if the key "z" is not in the dictionary d?
- A.Returns None
- B.Returns 0
- C.Raises KeyError
- D.Adds "z" with the value None
Answer & explanation
Correct answer: Raises KeyError
Square brackets need the key to exist. Use d.get("z") to get None instead of an error.
Can you get the rest right without peeking?
Take the timed Quiz: Python Dictionaries quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →