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:
Work out the value on the right of the =. Here it is already a value: 2.
Take one free location in memory and keep it for this program.
Write the name from the left of the = on the box, and put the value inside.
= 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:
a = 2
b = 3
c = a + b
print(c)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.
= runs.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:
score = 10
score = 25
print(score)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:
a = 5 tells Python to put 5 into a box called a. It has no answer, so nothing is displayed.
a == 5 asks whether what is in a equals 5. Its answer is True or False.
a = 5
print(a == 5)
print(a == 7)True False
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.50 = marksSyntaxError: 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:
print(total)
total = 88NameError: 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.
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.
print(p) at the very end. The principal is still in its box, untouched — using a variable never empties it.8Recap
= 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.What does Python do first when it runs c = a + b?
score = 10 is followed by score = score + 5. What does score point at now?
Which of these lines will Python refuse to run?
What is the difference between a = 5 and a == 5?