Variable Initialization and Assignment
Variable updates are not special operations; they follow the same evaluation rules as all assignments: Python evaluates the right side first, then stores the result in the variable on the left.
Try it: Assignment, Statement by Statement
How an assignment statement evaluates its right side first (using the names' current values) and only then binds the name on the left: updating x = x + 1, augmented operators like += and //=, chained a = b = 0, simultaneous a, b = b, a + b versus one-at-a-time assignment, rebinding a name to a value of another type, the NameError for a name that was never assigned, and the SyntaxError for a name that breaks the naming rules.
How it works
- Before anything runs, Python compiles the whole program; a name that breaks the rules (starts with a digit, contains a space or a dash, or is a keyword) is a SyntaxError and no line runs at all.
- For each assignment, evaluate the complete right side first, looking every name up in the variable table as it is now.
- Then bind the name(s) on the left to that value: a new name is added to the table, an existing one is replaced (even by a value of a different type).
- x += 5 reads x, applies the operator (+, -, *, //, %, **) and binds the result back to x; a = b = 0 evaluates 0 once and binds a, then b.
- a, b = b, a + b builds the whole right-hand tuple from the OLD values before binding a and b; two separate statements would use the new a in the second line.
- Reading a name that is not in the table raises NameError and the program stops at that line.
Default run (10 steps): A 4-line program. Python first compiles all of it, then runs it top to bottom. The variable table on the right starts empty. … Program finished. Final table: x = 16.
Simplified: The programs are fixed templates: you choose the template, its numbers, the operator and the variable name, never the code. The lab's own small Python tokenizer and parser (written for the statements these programs use: assignments, print calls, if / elif / else, for, while, def and return) checks the text; nothing is executed as real Python. Every program the lab can show was compiled by CPython 3.12 and the lab reproduces its exception type, message, line, caret columns and the printed report. A small interpreter written for the same statements then runs them (integers of any size, the str/float results these templates produce, and print); each program was also run in CPython 3.12 under sys.settrace, and the lab reproduces the variables after every line, the output and the error.
Loading the simulation…
Why a Starting Value Matters
An update changes a variable based on its current value. That means Python must be able to find a current value before it can calculate the next one. Initialization supplies this starting value. If you try to update a variable that has not been initialized, Python cannot find its current value and raises a NameError.
Preparing a variable for an update
A variable must increase by one. What must happen before the increase can be calculated?
- 1Initialize
Give the variable an initial value so Python has a current value to look up.
- 2Look up
When the update is evaluated, Python reads the variable's existing value.
- 3Calculate
Python performs the requested arithmetic using that current value.
- 4Store
Python stores the calculated result in the variable on the left.
Initialization is required because an update depends on the variable's current value.
The Evaluation Order
An assignment has two important sides. The right side contains the expression Python must evaluate, and the left side identifies where the result will be stored. Python evaluates the right side first. Only after the result has been calculated does Python store that result in the variable on the left.
This order explains how a variable can appear on both sides of an assignment. On the right, Python reads the variable's current value. It then performs the arithmetic. On the left, it stores the newly calculated value. The old value is therefore used as input before the new value replaces it.
What do you think happens?
When an update uses a variable on both sides, which happens first?
Reveal answer
The current value is read and the right side is calculated before the result is stored.
Python evaluates the right side of an assignment first. The resulting value is then stored in the variable on the left.
Following a Value Through an Update
Consider a variable whose current value is 5 and an update that adds 1. The update can be traced as three connected actions: Python looks up 5, calculates 5 plus 1 to obtain 6, and stores 6 as the variable's new value. The update is not a separate kind of operation; it follows the same evaluation rules as an assignment.
Tracing a repeated update
A variable starts with the value 2 and is increased by 1 twice. What value does it have after the two updates?
- 1Initial state
The variable begins at 2, so Python has a current value available for the first update.
- 2First update
Python reads 2, adds 1, and stores 3.
- 3Second update
Python now reads the current value 3, adds 1, and stores 4.
After two updates, the variable's value is 4.
Increment and Decrement
| Operation | Arithmetic change | Effect on current value |
|---|---|---|
| Increment | Add 1 | The value increases by one |
| Decrement | Subtract 1 | The value decreases by one |
Both operations require the variable to have an existing current value before the update is evaluated.
An increment adds 1 to the current value. A decrement subtracts 1 from the current value. Their directions differ, but their evaluation process is the same: Python looks up the current value, performs the arithmetic on the right side, and stores the result on the left.
Mistakes to Avoid
Common mistake Trying to update a variable before giving it an initial value
What it looks likeAn update attempts to read a variable whose current value does not exist.
Why it failsThe update depends on looking up the variable's current value. Without initialization, Python cannot find that value and raises a
NameError.Do this insteadInitialize the variable before applying an increment or decrement.
Common mistake Assuming the left side is updated before the right side is evaluated
What it looks likeTreating the variable on the left as though it already contains the new result while the arithmetic is still being calculated.
Why it failsPython evaluates the right side first and stores the result on the left afterward.
Do this insteadTrace every update in this order: look up the current value, perform the arithmetic, and store the result.
Common mistake Confusing an increment with a decrement
What it looks likeDescribing subtraction of 1 as an increase.
Why it failsAn increment adds 1, while a decrement subtracts 1.
Do this insteadCheck the arithmetic direction: addition raises the value and subtraction lowers it.
A variable has the current value 8. Trace one decrement and then one increment. State the value read for each update, the arithmetic performed, and the value stored after each update.
Hints
- The decrement subtracts 1 from the current value.
- After the decrement, use the new value as the current value for the increment.
- Keep the read, calculate, and store stages separate.
Key Takeaways
- 1Initialize a variable before updating it so Python can find its current value.
- 2Python evaluates the right side of an assignment before storing the result in the variable on the left.
- 3A variable update follows the sequence look up, calculate, and store.
- 4An increment adds 1, while a decrement subtracts 1.
- 5The same evaluation rules apply to both increments and decrements.
At a glance
- A variable must have an initial value before Python can update it.
- Python reads and evaluates the right side before storing the result on the left.
- Every update can be traced as looking up the current value, calculating, and storing the new value.
- Incrementing adds 1; decrementing subtracts 1.