LambdaLabTM
Computer Science · Class 11 · Variables & User Input
VariablesMemory⏱️ 9 min read

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.

The definition to remember
A variable is a named memory location used for storing a value. The name is what lets you reach that value again, as many times as you want, from anywhere later in the program.

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:

1. A value to store — the literal

The value you want to keep: 2, 49.5, 'Aarav'. The value stored in a variable is called a literal.

2. A name for the box — the identifier

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:

first_variable.py
a = 2
print(a)
Output
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.

Click a part of the statement to find it in memory

The box is labelled a, and the value 2 is inside it.

Memory (RAM) — a wall of empty boxes
a2

One box, out of the millions in RAM, is now labelled a and holds 2. That box is the variable.

Three words, one line of code
In 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:

using_boxes.py
a = 2
b = 3

print(a + b)
print(a * 10)
print(a)
Output
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.

A name in quotes is not a variable
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.
quotes_matter.py
a = 2
print(a)
print('a')
Output
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.py
score = 10
print(score)
score = 25
print(score)
Output
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

Key Takeaway
A variable is a named memory location used for storing a value. The name given to it is called its identifier, and the value stored inside it is called a literal. Variables are made in the computer's RAM, they can be read any number of times, and the value inside them can be changed.
Quick Check

A variable is best described as…

Quick Check

In the statement price = 49.5, what is 49.5 called?

Quick Check

In the statement price = 49.5, what is price called?

Quick Check

After a = 2, what does print(a) show — and what does print('a') show?