Names Are Not Boxes
Most explanations call a variable a box that holds a value, and that picture breaks the moment one name affects another. Point two names at the same list, change it through one, and watch the other see it too.
Most introductions call a variable a box that holds a value. That picture works right up until you assign one name to another — and then it actively misleads you.
A name pointing at a value, not holding it
Run this, and predict the output before you check it:
a = [1, 2, 3]b = ab.append(4)print(a)If a variable were a box, a would still read [1, 2, 3] — you only changed b. But a prints [1, 2, 3, 4]. Line 2 did not copy the list into a second box. It pointed a second name at the same list. There was only ever one list in this program.
a and b point at the same list right now. Append through b and watch what a sees.
Reassigning a name never touches the value
Now the case that actually reassures people. If a and b both point at 3, and you write a = 5, does b change too?
a = 3b = aa = 5print(b)# 3It does not, and the toggle above shows why. a = 5 does not reach into the number 3 and edit it — numbers cannot be edited. It points the name a at a different value entirely and leaves b exactly where it was. Assignment always moves an arrow. It only looks like mutation when the arrow happens to point at something, like a list, that can be changed from where it sits.
Names Python will not let you use
A name can contain letters, digits, and underscores, but cannot start with a digit, and cannot be one of Python's reserved words — if, for, class, and about thirty others that mean something fixed to the language itself.
$ 2nd_place = "Alice" File "<stdin>", line 1 2nd_place = "Alice" ^$ SyntaxError: invalid decimal literalKey takeaways
- A variable is a name pointing at a value, not a box that contains one.
- Two names can point at the same value — change it through one, and the other sees the change too.
- Reassigning a name moves that name's arrow. It never edits the value the arrow used to point at.
- Names use letters, digits, and underscores; cannot start with a digit; cannot be a reserved word.
- snake_case is the convention every Python style guide expects for variable names.