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.
This only ever shows up with values you can change after they exist, like a list. Numbers, strings, and tuples cannot be edited in place at all — later lessons call that immutable — so pointing two names at one of those never produces a surprise like this. The box picture happens to work for those values by accident, which is exactly what makes it dangerous: it stops working the moment you touch something mutable, and nothing warns you in advance which kind you are holding.
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.
Same object, or just equal values
Two names can point at separate values that happen to look identical. Python has two different operators for the two different questions this raises, and treating them as interchangeable is a quiet source of bugs.
$ >>> a = [1, 2, 3]$ >>> b = [1, 2, 3]$ >>> a == b$ True$ >>> a is b$ Falsea and b here are two separate lists that happen to hold the same three numbers — equal, but not the same object. Compare that with what you already saw above: assign b = a directly, and there is only one list on two names.
$ >>> a = [1, 2, 3]$ >>> b = a$ >>> a is b$ True==
Equality. Asks “do these hold the same value?” — the question you want almost all of the time, for numbers, strings, lists, anything.
is
Identity. Asks “are these literally the same object?” — the question behind the bug above, and behind idiomatic checks like x is None.
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 literalNames are also case-sensitive, which trips people up in a quieter way than a SyntaxError does — Python treats score and Score as two entirely unrelated names, not two spellings of the same one.
$ >>> score = 90$ >>> Score$ NameError: name 'Score' is not definedKey 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, but only if the value is mutable.
- Reassigning a name moves that name's arrow. It never edits the value the arrow used to point at.
- == compares values; is compares identity. Use == unless you specifically need to know they are the exact same object.
- Names use letters, digits, and underscores; cannot start with a digit; cannot be a reserved word; and are case-sensitive.
Quick check
Answer these to unlock the next chapter — 3 of 4 to pass. You can retake it anytime.
Answer every question to check.
Make a free account to read on
Every chapter is free — an account is how your progress, XP, and streak follow you from your laptop to your phone, and how you show up on the leaderboard. No payment, no trial.