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.

Lesson 2 of 5PROAges 10+
See what PRO unlocks →

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

25

You 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

fire

A 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"] - 15
  • It reads the current hp (40), subtracts 15, and stores 25 back under the hp key
  • It stores the TEXT "dragon[\"hp\"] - 15" under the hp key

Why: 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"?

  • It creates a brand new key called "element" with the value "fire"
  • It causes an error, since "element" doesn't exist yet

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.

Unlock this lesson with PRO →

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 →