What does this print?
user = {"name": "Ana"}
print(user.get("age"))Options
- KeyError
- None (Correct option)
- 0
- ""
Answer
None
Why
A dictionary stores key → value pairs. user["age"] would raise KeyError because that key does not exist.
.get("age") is the safe way: it returns None when the key is missing. You can also choose a default: user.get("age", 0) returns 0.
Remember
dict.get(key, default) avoids KeyError.