CurriculumPython Variables for KidsChanging what's remembered

How to Update a Python Variable Using Its Own Value

Boxes can be updated — reading the old value to make the new one.

Lesson 2 of 5FreeAges 10+
Try this lesson interactively →

Changing what's remembered

A box doesn't have to keep the same thing forever. You can change what's inside it.

Here the dragon starts with 50 health.

health = 50
print(health)

Output

50

To heal, you read what's in the box, ADD to it, then save the new number back in the SAME box.

health = health + 20

Read this right to left: work out health + 20 first (that's 70), THEN put 70 into health.

health = health + 20

Print it again and you get the new number. The box remembers the change.

health = 50
health = health + 20
print(health)

Output

70

One trap to know about: writing health = 20 does NOT add 20. It REPLACES everything with just 20.

health = 50
health = 20
print(health)

Output

20

Check your understanding

health starts at 30. What does health = health + 10 make it?

health = 30
health = health + 10
print(health)
  • 40
  • 10
  • health + 10

Why: 30 + 10 = 40, and that 40 gets saved back into health. The box's OLD value is used to make the NEW one.

gold starts at 5. What does gold = 20 make it — not gold = gold + 20, just gold = 20?

gold = 5
gold = 20
print(gold)
  • 20
  • 25
  • 5

Why: gold = 20 with no + throws away the old 5 completely and puts 20 in its place. Compare that to gold = gold + 20, which would have kept the 5 and added to it.

What you'll practice

The dragon's health is 50. It rests by a warm fire and gains 20 health. Show both — what it was, and what it becomes.

Start this lesson for real →

More from this topic

Changing what's remembered is one lesson inside Python Variables for Kids — see the full lesson order and what the whole topic covers.

View the full topic →