CurriculumPython Dictionaries for KidsUpdating a stat, adding a new one
How to Update and Add Keys in a Python Dictionary
Read the old value to make the new one — and setting a NEW key creates it on the spot.
Updating a stat, adding a new one
Just like a variable, a dict's value can change. Same pattern you already know.
dragon["hp"] = dragon["hp"] - 15 reads the CURRENT hp, subtracts 15, and stores that back under "hp".
dragon = {"hp": 40}
dragon["hp"] = dragon["hp"] - 15
print(dragon["hp"])Output
25You can also add a key that never existed before, just by setting it: dragon["element"] = "fire" creates a brand new key.
dragon["element"] = "fire"
print(dragon["element"])Output
fireA dict grows by naming a new key — no special command needed, unlike a list's append().
Check your understanding
dragon = {"hp": 40}. What does dragon["hp"] = dragon["hp"] - 15 do?
dragon = {"hp": 40}
dragon["hp"] = dragon["hp"] - 15Why: It reads 40, subtracts 15, and stores 25 back under "hp" — the same "compute first, store the result" rule as any variable update.
dragon = {"name": "Ember"} has no "element" key. What happens if you write dragon["element"] = "fire"?
Why: It creates the "element" key on the spot, with the value "fire". A dict grows just by naming a new key and giving it a value.
What you'll practice
dragon = {"name": "Ember", "hp": 40}. Print the hp, then set dragon["hp"] to dragon["hp"] minus 15 (damage taken), then print hp again — showing the change.
More from this topic
Updating a stat, adding a new one is one lesson inside Python Dictionaries for Kids — see the full lesson order and what the whole topic covers.
View the full topic →