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.
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?
Initialize: Give the variable an initial value so Python has a current value to look up.
Look up: When the update is evaluated, Python reads the variable's existing value.
Calculate: Python performs the requested arithmetic using that current value.
Store: 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
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?
Initial state: The variable begins at 2, so Python has a current value available for the first update.
First update: Python reads 2, adds 1, and stores 3.
Second 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 |
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
Trying to update a variable before giving it an initial value
The update depends on looking up the variable's current value. Without initialization, Python cannot find that value and raises a NameError.
Fix:
Initialize the variable before applying an increment or decrement.Assuming the left side is updated before the right side is evaluated
Python evaluates the right side first and stores the result on the left afterward.
Fix:
Trace every update in this order: look up the current value, perform the arithmetic, and store the result.Confusing an increment with a decrement
An increment adds 1, while a decrement subtracts 1.
Fix:
Check 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
- Initialize a variable before updating it so Python can find its current value.
- Python evaluates the right side of an assignment before storing the result in the variable on the left.
- A variable update follows the sequence look up, calculate, and store.
- An increment adds 1, while a decrement subtracts 1.
- The same evaluation rules apply to both increments and decrements.
Key Takeaways
- 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.