Fill: Dict Get with Default
Fill in the Blank Easy
Question
Fill in the blank to get the value for key 'x' or 0 if missing.
Code
d = {"a": 1}
val = d.___("x", 0)
Hint
The method that returns a default instead of raising KeyError is...
Explanation
get(key, default) returns the value for the key, or the default if the key is missing. d.get('x', 0) returns 0 because 'x' is not in the dict.
Related Resources
Related Lessons
- Dictionaries
Safe access with get().
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.