Repaso
Español
PythonCorePY-49

What does this print?

user = {"name": "Ana"}
print(user.get("age"))

Options

  1. KeyError
  2. None (Correct option)
  3. 0
  4. ""

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.

Key terms

Practice this question in Repaso