What is a Variable?
A variable is a box — a small space in the computer's memory that your program keeps a value in, with a name written on it so you can find it again. That is the whole idea. Everything else in this lesson is just the vocabulary.
1Memory is a wall of empty boxes
While your program is running, it lives in the computer's memory — the RAM. Picture RAM as an enormous wall of empty boxes, millions of them, all identical and all unused.
When your program needs to keep a value, it takes over one of those boxes: it reserves it, sticks a label on it, and drops the value inside. That reserved, labelled, filled box is what we call a variable.
2To make one, you need two things
Python cannot reserve a box out of nothing. You have to give it two things, and each of them has a proper name:
The value you want to keep: 2, 49.5, 'Aarav'. The value stored in a variable is called a literal.
The label you stick on the box so you can identify it later: a, total, price. The name of a variable is called its identifier.
Put both together with an = between them, and the variable exists:
a = 2
print(a)2
Line 1 shows nothing on the screen. It was not a question, so Python had no answer to give — it quietly went off and reserved a box. Line 2 is where we ask to see what is inside the box.
3Take one apart
Click each part of the statement below. A green tick lands on the memory wall to the right, showing exactly what that part becomes once Python has run the line.
The box is labelled a, and the value 2 is inside it.
One box, out of the millions in RAM, is now labelled a and holds 2. That box is the variable.
a = 2: a is the identifier, 2 is the literal, and the box in memory that ends up labelled a and holding 2 is the variable. The = is the assignment operator that made it happen — the whole of the next lesson.4Using the name in place of the value
Once a variable exists, you can write its name anywhere you would have written the value. Python looks inside the box and uses whatever it finds:
a = 2
b = 3
print(a + b)
print(a * 10)
print(a)5 20 2
Reading a variable does not empty it. a was used three times and still holds 2 — a box can be read as often as you like. That is exactly the reuse we were missing in the last lesson.
print(a) shows what is inside the box. print('a') just prints the letter a. Quotes mean “this is text, take it as it is” — so Python never goes looking for a box at all.a = 2
print(a)
print('a')2 a
5Why is it called a variable?
Because what is inside the box can vary. The label stays put, but the value in the box can be swapped for a new one whenever the program needs to:
score = 10
print(score)
score = 25
print(score)10 25
Same box, same label, new value. The old 10 is simply gone. We will watch this happen in memory in the next lesson.
6Recap
A variable is best described as…
In the statement price = 49.5, what is 49.5 called?
In the statement price = 49.5, what is price called?
After a = 2, what does print(a) show — and what does print('a') show?