6.1. Updating variables¶
Commonly, assignment statements are used to update a variable, where the new value of the variable depends on the old.
x = x + 1
This means “get the current value of
x, add 1, and then
x with the new value.”
If you try to update a variable that doesn’t exist, you get an error,
because Python evaluates the right side before it assigns a value to
Before you can update a variable, you have to initialize it, usually with a simple assignment:
When you update a variable by adding 1 it’s called an increment; subtracting 1 is called a decrement.