Variables, the Labeled Boxes
Here is the plain idea: a variable is a labeled box. You put a value inside it, write a name on the front, and from then on you can ask for that value by name instead of remembering the value itself.
Write age = 30, and you've put the number 30 in a box labeled age. Later, anywhere you write age, the computer fetches what's inside. The = doesn't mean "equals" the way it did in math class — it means "put the thing on the right into the box on the left."
The labeled jar analogy
Picture a pantry shelf full of jars. One jar reads "sugar," another reads "flour," another reads "salt." You don't peer inside every jar to find what you need — you read the label and reach for the right one.
A variable is one of those jars. The label is the name you choose. The contents are the value. When your recipe says "add the sugar," it doesn't care exactly how much is in the jar right now. It just grabs whatever the sugar jar holds.
You can change what's inside
Here's where the jar picture really pays off. You can empty a jar and refill it. Write age = 30, and later age = 31, and the box now holds 31. The old value is painted over and gone; the label stays the same, the contents change.
This is why they're called variables — the value can vary. The name is a stable handle, and what it points to can change over the life of the program. A score that climbs, a temperature that updates, a name a user types in — all boxes whose contents change while the label stays put.
A box can even refill itself from its own old value: total = total + 5 tells the computer to read what's in total right now, add five, and pour the answer back in. That one trick is how a program keeps a running tally of anything.
Why use a name at all?
Two reasons, both practical.
First, memory. A program might juggle hundreds of values. Names let you refer to each one without holding the actual value in your head. You think "the total price," not "the number 47.99."
Second, change in one place. If you store a tax rate in a box called tax_rate and use that name everywhere, then changing the rate means refilling one jar — not hunting down the number sprinkled across the whole program.
And one kindness worth starting now: pick honest labels. A jar marked "stuff" helps no one, and names like x or temp tell you nothing a week later. Names like user_age or total_price read almost like plain English, and the next person to read the code — usually you — will thank you.
Your turn
No computer needed, just a pencil. Trace these lines one at a time and write down what the box holds after each:
candles = 6
candles = candles + 4
candles = candles - 1
Work top to bottom, updating the jar on each line. What is in candles at the end? (The answer is 9. If you got it, you already understand how a variable holds a value and changes it.)
So: a variable is a labeled box, the name is how you reach it, and the value inside can change. Next we'll look at what kinds of things those boxes can hold. 🐙
Stuck or curious?
Ask Pip about this lesson — tap the porthole bottom-right.