Give a value a name
Quick review (2 minutes)
Think of a coat check.
An assignment has a name to the left of = and a value or calculation to the right. Python works out the whole right-hand side first, then ties the left-hand name to the result. If that name already had a value, it now names the new one instead. No other name changes.
Draw a state table with a column for the line, a column for what the right-hand side works out to, one column for each name and a column for output.
Take the next line of the program. If it is an assignment, cover the left side with your finger and work out the right side, using the values in the row above.
Write that result in the column of the name on the left. Carry every other name's value down unchanged from the row above.
If the line is a
print, write what it displays in the output column: the values in the order given, with one space between them.Repeat until the last line. The output column is your prediction. Compare it with the real output if you have Python 3.13, or with the printed answer if you are working on paper.
The usual mistake is to predict 5 5. It comes from reading spare = boxes as a lasting link, as if spare will always match boxes. That line ran once, at step 2, and tied spare to 4. Step 3 moved only the boxes tag, because boxes was the only name on the left.
A second mistake is reading = as the equals sign from arithmetic. Read that way, boxes = boxes + 1 looks like nonsense, since no number equals itself plus one. In Python, = means "tie this name to this value". The comparison "is equal to" is written with two signs, ==, and a later unit teaches it.
Avoid saying that spare = boxes "makes a copy". The Python tutorial states that a simple assignment never copies data. It puts a second name on the value that is already there. This lesson keeps to whole numbers on purpose.
1 In plain words
Think of a coat check. The attendant ties a paper tag to your coat, and the tag tells everyone which coat is yours. Tie a second tag to the same coat and both tags lead to it. Move the first tag to a different coat and the second tag stays where it was, because nobody touched it.
Python names work like those tags, and the things they hang on are values. The line boxes = 4 reads aloud as "boxes now names 4", not "boxes equals 4". It is an instruction Python carries out, and nothing appears on the screen when it does.
A value is a piece of data. In this lesson every value is a whole number such as 4 or 5, which Python calls an int. A name, also called a variable, is a word you attach to a value so you can use that value again later.
In this lesson, every assignment is a line with one = sign: a name on the left, and a value or a calculation on the right. Everything after the = is the right-hand side. Giving a new value to a name that already has one is called reassigning, and the Python language reference calls it rebinding.
To trace a program means to run it by hand, one line at a time, in the order Python would. A state table is where you record the trace: one row for each instruction and columns for the right-side value, each name and output, showing its value after that line runs.
You met the shell and the editor in lesson 1. In the shell you type a line after the >>> prompt, and Python answers on the next line. In the editor you type a whole file, save it and run it. Every example here targets Python 3.13. You can follow along on your own computer, or with a pencil and the printed trace.
Remember one sentence: work out the right side first, then move the left-hand tag. To look it up, see section 3.1.1 "Numbers" of the Python 3.13 tutorial and section 7.2 "Assignment statements" of the Python 3.13 language reference.
2 Picture it
The boxes and spare tags start together at 4. After boxes changes to 5, the spare tag stays at 4.
3 Why
Python treats = as an order, not a question about whether two sides match. At an assignment, Python first works out the right-hand side to get one value. Only then does it attach the name on the left to that value.
That order is what makes boxes = boxes + 1 sensible. As an equation it could never be true. As an instruction it is clear: look up the value boxes names now, which is 4, add 1 to get 5, then move the boxes tag to 5. When a name already has a value, the assignment rebinds it, and the old value loses that tag.
Only the name on the left moves. spare = boxes asks for the value boxes names at that moment and ties spare to it. It does not tie spare to the word boxes, so a later line that moves boxes leaves spare where it was.
4 The rule
An assignment has a name to the left of = and a value or calculation to the right.
Python works out the whole right-hand side first, then ties the left-hand name to the result. If that name already had a value, it now names the new one instead. No other name changes.
5 How
- Draw a state table with a column for the line, a column for what the right-hand side works out to, one column for each name and a column for output.
- Take the next line of the program. If it is an assignment, cover the left side with your finger and work out the right side, using the values in the row above.
- Write that result in the column of the name on the left. Carry every other name's value down unchanged from the row above.
- If the line is a
print, write what it displays in the output column: the values in the order given, with one space between them. - Repeat until the last line. The output column is your prediction. Compare it with the real output if you have Python 3.13, or with the printed answer if you are working on paper.
6 Worked example
Trace boxes and spare
boxes = 4
spare = boxes
boxes = boxes + 1
print(spare, boxes)
Standard input
Empty
Standard output
4 5
Follow the rows below. For a check in the Shell, enter the three assignments and then enter each name on its own.
Trace
| Instruction | Right side | boxes | spare | Output | Explanation |
|---|---|---|---|---|---|
| boxes = 4 | 4 | 4 | unset | — | The first line gives boxes the value 4. |
| spare = boxes | 4 | 4 | 4 | — | Look up boxes first. Give spare that value. |
| boxes = boxes + 1 | 5 | 5 | 4 | — | Calculate 4 + 1 before giving boxes its new value. |
| print(spare, boxes) | — | 5 | 4 | 4 5 | Print reads spare first and boxes second. |
7 The trap
The usual mistake is to predict 5 5. It comes from reading spare = boxes as a lasting link, as if spare will always match boxes. That line ran once, at step 2, and tied spare to 4. Step 3 moved only the boxes tag, because boxes was the only name on the left.
A second mistake is reading = as the equals sign from arithmetic. Read that way, boxes = boxes + 1 looks like nonsense, since no number equals itself plus one. In Python, = means "tie this name to this value". The comparison "is equal to" is written with two signs, ==, and a later unit teaches it.
Avoid saying that spare = boxes "makes a copy". The Python tutorial states that a simple assignment never copies data. It puts a second name on the value that is already there. This lesson keeps to whole numbers on purpose.
Quick checks
Quick check 1 of 2
Trace this program with a state table before you run it. If you run it, type the four lines in the editor and save them as cups.py. The program reads no input. What does it display?
cups = 7
extra = cups
cups = cups + 3
print(extra, cups)Show the steps and the answer
7 10
Why: Step 2 ties extra to 7, the value cups names at that moment. Step 3 works out 7 + 3 = 10 first, then moves only cups. extra stays on 7, and print shows extra, a space, then cups.
Source: Python Software Foundation, The Python Language Reference (3.13): 7.2 Assignment statements
Quick check 2 of 2
In this program the right-hand side of a later line uses a different name. If you run it, type the four lines in the editor. The program reads no input. What does it display?
seeds = 2
pots = seeds + 3
seeds = pots
print(seeds, pots)Show the steps and the answer
5 5
Why: pots gets 2 + 3 = 5. In seeds = pots the right side is worked out first, and it is the value pots names now, 5. The seeds tag moves to 5, so both names end on 5.
Source: Python Software Foundation, The Python Language Reference (3.13): 7.2 Assignment statements
Sources
- The Python Tutorial (3.13): 3. An Informal Introduction to Python. Python Software Foundation. PSF License Version 2. Read 25 Nov 2026.
- The Python Language Reference (3.13): 7.2 Assignment statements. Python Software Foundation. PSF License Version 2. Read 25 Nov 2026.