Module 2 · Variables · Lesson 5 of 8
Assigning a new value
A box isn't sealed. Assign to a name that already exists and you replace what's inside — the old value is simply gone. The newest assignment always wins.
The really useful move is updating a variable from its own current value, like counter = counter + 1. It looks circular, but it isn't: Python works out the right side first — reading the old value — and only then stores the result back.
Old value in, new value out
counter = 0counter = counter + 1
counter
0
0 + 1 = 1So if counter held 2, the right side becomes 2 + 1, which is 3, and that 3 goes back into the box. Run it for yourself:
Run it yourselfruns in your browser · Python
main.py
And yes — because Python boxes are data-shaped, a reassignment can change the type too: a counter that held a number can suddenly hold the text "done". Handy, but worth keeping track of.