The dragon explains
A missing key crashes — .get() doesn't have to
The dragon: One more thing about keys: asking for one that doesn't exist is a real crash.
The dragon: dragon["mana"] raises a KeyError if "mana" was never set — Python won't guess a value for you.
example.pyPython 3dragon = {"name": "Ember"}print(dragon["mana"])
The dragon: .get(key, default) asks SAFELY — if the key is missing, you get your own chosen default instead of a crash.
example.pyPython 3print(dragon.get("mana", 0))Output0The dragon: If the key DOES exist, .get() just returns the real value — the default only kicks in when it's actually missing.
Quick check
Check your understanding
Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.
Question 1 of 2
dragon = {"name": "Ember"}. What happens with print(dragon["mana"])?
question.pyPython 3dragon = {"name": "Ember"}print(dragon["mana"])
- A KeyError — "mana" was never set as a keyCorrect
- It prints None, since the key is empty
Why: A KeyError. dragon["mana"] with square brackets crashes on a missing key — .get() is the safe alternative when a key might not be there.
Question 2 of 2
dragon = {"hp": 70}. What does dragon.get("hp", 0) return?
question.pyPython 3dragon = {"hp": 70}print(dragon.get("hp", 0))
- 70 — the real value, since "hp" existsCorrect
- 0 — the default, since .get() always uses it
Why: 70. The default in .get(key, default) is a FALLBACK — it only shows up when the key genuinely isn't there.
In the game
What you'll practice
Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.
- 1
GuidedYour first try
dragon = {"name": "Ember", "hp": 40}. This code crashes with a KeyError trying to print dragon["mana"] — a key that was never set. Run it once to see the real error, then fix it with dragon.get("mana", 0) so it prints 0 instead of crashing.
- +2
PracticeTwo more, on your own
This lesson is part of PRO.
PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.
Unlock this lesson with PROStuck three times? The game offers the answer — unasked.