Variables and State
Learn how a name refers to a value, and how a program's state changes as names are reassigned.
By the end of this lesson, you can
- Trace a sequence of assignments and predict a variable's value at each step
- Distinguish a variable name from the value it currently refers to
- Explain why = means assignment in Python, not mathematical equality
Why it matters
Programs rarely work with just one value in isolation — they keep track of information that changes as the program runs: a running total, a player’s score, whether a form has been filled in. A variable is how a program holds onto and updates that changing information, which programmers call state. Almost every bug involving “the wrong number showed up” is really a question of what a variable’s state was at a particular moment.
Mental model
A variable is a name that refers to a value — not a labeled box that a value gets stuffed inside. When you write:
score = 0read it as “the name score now refers to the value 0,” not “0 goes
into the score box.” The difference matters once reassignment enters the
picture:
score = 0
score = score + 10
score = score + 5
print(score)15Nothing is being edited in place here. Each score = ... line computes a
brand-new value on the right-hand side, then makes the name score
refer to that new value instead. The old value (0, then 10) isn’t
modified — it’s simply no longer referred to by anything, so it’s
forgotten.
Trace it
Tracing means writing down a variable’s value after every line that could change it. For the program above:
| Line | Code | score |
|---|---|---|
| 1 | score = 0 | 0 |
| 2 | score = score + 10 | 10 |
| 3 | score = score + 5 | 15 |
Notice that line 2 reads score (its old value, 0) before replacing
it. The right-hand side of an assignment is always fully worked out
first, using whatever the names currently refer to — only then does the
assignment happen.
Check your understanding
Or reveal the answer without checking
Answer:5
y = x makes y refer to the value 5 at that moment. Later reassigning x to 9 only changes what x refers to — y still refers to 5, unaffected.
Or reveal the answer without checking
Answer:count = 3 assigns; count == 3 checks whether count currently equals 3 and produces True or False
A single = always assigns. A double == always compares and produces a boolean result, without changing anything.
Practice: warm-up
Trace this program by hand — write down the value of total after each
line — before revealing the answer.
total = 100
total = total - 20
total = total - total
Stuck? Reveal one hint at a time.
Hint 1
Work top to bottom. Each line only changes total once you finish evaluating its right-hand side.
Hint 2
On the last line, total appears on both sides — use whatever value it refers to right before that line runs.
Reveal the trace
Try the problem yourself before reading this. There is often more than one reasonable approach — treat this as one worked example, not the only correct answer.
Line 1: total = 100 -> total is 100
Line 2: total = total - 20 -> total is 80
Line 3: total = total - total -> uses the current total (80) on both
sides: 80 - 80 = 0, so total is 0Practice: apply it
This program is supposed to swap the values of a and b, but it has a
bug:
a = 1
b = 2
a = b
b = a
print(a, b)
Or reveal the answer without checking
Answer:2 2
a = b makes a refer to 2 — but that overwrites the only name that still remembered 1. By the time b = a runs, a is already 2, so b becomes 2 as well. The original value of a is gone.
Modification challenge: fix the swap so it prints 2 1. (Hint: you
need a third variable to temporarily hold one of the values before it’s
overwritten.)
Summary
- A variable is a name that refers to a value — reassigning it changes what it refers to, not the old value itself.
=always means “evaluate the right side, then bind the name on the left to that result.” It is never a claim of mathematical equality.- The right-hand side of an assignment is fully evaluated using each name’s current value before the assignment happens.
==checks equality and producesTrueorFalse; it never assigns anything.- Tracing — writing down a variable’s value after every line that touches it — is the most reliable way to catch a wrong assumption about state.