LambdaLabTM
Computer Science · Class 11 · Variables & User Input
VariablesOperators⏱️ 10 min read

The Assignment Operator (=)

A variable does not appear by magic. One small symbol makes it — the =, called the assignment operator. This lesson is about the three things it does, in the order it does them.

1What the = really does

When Python runs the line a = 2, it does three jobs, one after the other:

step 1
Solve the right side

Work out the value on the right of the =. Here it is already a value: 2.

step 2
Reserve a box

Take one free location in memory and keep it for this program.

step 3
Label it, fill it

Write the name from the left of the = on the box, and put the value inside.

Key Takeaway
= means “put into”, not “is equal to”. Read a = 2 out loud as “a gets 2” or “put 2 into a”. The name being labelled is always on the left, and the value being stored is always on the right.

2The right side is always solved first

The value on the right does not have to be a plain literal. It can be a whole expression — even one built out of other variables. Python solves it completely, down to one single value, and only then labels the box:

sum.py
a = 2
b = 3
c = a + b
print(c)
Output
5

On line 3, Python does not store the words a + b in the box. It looks inside a, finds 2. It looks inside b, finds 3. It adds them to get 5. Only that 5 goes into c.

3Watch memory fill up, line by line

Step through the program below and watch the memory wall on the right. Boxes only appear when an = runs. Then switch to the second tab, which shows the line that confuses everybody the first time they see it: score = score + 5.

a = 2
b = 3
c = a + b
print(c)
Screen
Memory (RAM)
Press Run line 1 and watch the memory wall on the right. Nothing exists there yet — the boxes only appear when an = runs.
Why score = score + 5 is not nonsense
In maths, score = score + 5 would be impossible — no number equals itself plus five. But = is not the equals sign here. Python solves the right side using the value the box holds right now (10 + 5 = 15), and then stores that new value and moves the label score onto it. It is not a claim. It is an instruction.

4One name, one box at a time

Using a name again does not change the old box. The new value is stored in a box of its own, and the name moves across to it. The old value stays right where it is — nothing rubs it out — but it has no label on it now, so no later line can ask for it:

replace.py
score = 10
score = 25
print(score)
Output
25

The 10 is still in memory, but with no name attached there is no way to ask for it back. If a value matters later, give it a name of its own.

5= is not the same as ==

You met == in the last chapter, and the two are easy to mix up. They do completely different jobs:

=
Assignment — an order

a = 5 tells Python to put 5 into a box called a. It has no answer, so nothing is displayed.

==
Comparison — a question

a == 5 asks whether what is in a equals 5. Its answer is True or False.

ask_or_order.py
a = 5

print(a == 5)
print(a == 7)
Output
True
False
The name goes on the left. Always.
50 = marks is an error. Python reads the left side as the label to write on a box, and 50 cannot be a label — you would be asking Python to rename the number fifty.
wrong_way_round.py
50 = marks
Output
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

6Using a box before it exists

A variable exists only after the line that creates it has run. Ask for a name Python has never labelled, and it stops with a NameError:

too_early.py
print(total)
total = 88
Output
NameError: name 'total' is not defined

The box is made on line 2 — but line 1 runs first, and at that moment no box called total exists yet. Swap the two lines and the program prints 88 happily. Python runs your file from top to bottom, so a variable must be created above the line that uses it.

Note
A NameError nearly always means one of two things: you used the variable before you created it, or you spelled the name differently the second time. Python matches labels letter for letter.

7Your turn

Here is the classic first program that uses variables — simple interest. Run it, then change the values in the boxes at the top and run it again. Notice that si is worked out once and used twice.

simple_interest.py
Tip
Try adding a line print(p) at the very end. The principal is still in its box, untouched — using a variable never empties it.

8Recap

Key Takeaway
The assignment operator = creates a variable: it solves the expression on its right, reserves a memory location, labels that location with the identifier on its left, and stores the value inside. Assigning again to the same name stores the new value in a new location and moves the name onto it. = gives an order; == asks a question.
Quick Check

What does Python do first when it runs c = a + b?

Quick Check

score = 10 is followed by score = score + 5. What does score point at now?

Quick Check

Which of these lines will Python refuse to run?

Quick Check

What is the difference between a = 5 and a == 5?