Learning LibraryCore Coding LibraryKids

Variables: labeled boxes your code remembers

A variable is a labeled box. You put a value inside, give the box a name, and your code remembers it — ready to use, and easy to change.

The big idea

A variable is a name that holds a value. Use that value later by writing its name.

See it in code

1Start simple

The simplest variable is one labeled box. We put "Blaze" in a box called name. After that, writing name gives the value right back:

python
name = "Blaze"
print(name)
Run it — the box hands back what we put inside:
Blaze

One box, one name. Say name anywhere and Python swaps in "Blaze" for you.

2A step further

A box is yours to refill. Put a new value in and the box forgets the old one — it only remembers what's inside now:

python
coins = 5
print(coins)
coins = 10
print(coins)
Run it — same box, brand new value:
5
10

The coins box went from 5 to 10. Refilling a box is how your code keeps track of things that change.

3In our world

Now a hero. We make a name box for "Blaze" and a health box for 100. Then the hero takes a hit, so we refill health with a smaller number:

python
name = "Blaze"
health = 100

print(name, "starts with", health, "HP")

health = health - 30
print("After a hit:", health)
Run it, and the boxes remember — even after we refill one:
Blaze starts with 100 HP
After a hit: 70

The health box started at 100, then we put 70 in it. Same box, new value — that is the superpower of a variable.

The same idea, everywhere

Every program uses variables, not just hero sheets. A score that climbs, a star's x spot, a color you use ten times — each one is a labeled box. Name a value once. Then the whole program can share it and change it in one place.

Try it yourself

Add a box called power holding 50, then print it. Next, give the hero a shield: make health go up by writing health = health + 20, and print it again to watch the number grow.

The common mistake

Mixing up the two sides of the =. The name always goes on the left, the value on the right. Writing 100 = health is backwards, and Python won't allow it — it is always health = 100.

What it unlocks

Once values have names, you can compare them with comparison operators, do math on them with operators, and show them with print().

Older, or want more depth? Read the Teens version →