Many Python beginners first hear that a variable is like a box, and the data is stored inside the box. This can feel helpful at the very beginning, but in Python, it can also create the wrong mental model.
A better way to think about variables is this: a variable name is more like a label. The object exists, and the name points to that object.
Common Mistake
Look at this line:
x = 123
Many students imagine a box named x, with the number 123 inside it.
The problem is that this makes x sound like a container. In Python, it is more accurate to say that 123 is a number object, and x is a name that refers to that object.
You can picture it like this:
x -> 123
x is not the box. x is the label attached to the object 123.
Core Idea
In Python, variable names refer to objects. In beginner-friendly language:
a variable name points to a data object.
When you write:
x = 123
you can think of two steps:
- There is a number object,
123. - The name
xpoints to that object.
This is why the label model is better than the box model.
What Happens When We Reassign?
Now look at this:
x = 456
With the box model, you may imagine taking 123 out of the x box and putting 456 inside.
With the label model, the idea is clearer: a new number object, 456, appears, and the label x now points to 456.
Before: x -> 123
After: x -> 456
The contents of a box did not get replaced. The name x now refers to a different object.
A Quick Code Check
We can use id() to observe object identity:
x = 123
print(id(x))
x = 456
print(id(x))
The exact output depends on your computer and Python runtime, but you will often see two different identities:
...different number...
...different number...
You do not need to memorize the details of id() right now. The important idea is that after reassignment, the name x refers to another object.
Comparison
| Mental model | Good for | What can go wrong |
|---|---|---|
| Variable as a box | Feels intuitive at first | Makes it seem like the variable itself stores the object |
| Variable as a label | Fits Python better | Helps with references, reassignment, and mutable lists |
Why This Matters
For x = 123, this may sound like a small difference. But when you learn lists, it becomes very important.
For example, later you will see code like this:
a = [1, 2, 3]
b = a
This does not necessarily create two separate list boxes. It can mean that two names, a and b, point to the same list object. Many beginner list bugs start from this misunderstanding.
The earlier you understand variables as names pointing to objects, the easier it will be to learn mutable objects, function parameters, and list behavior.
Practice
- Draw the label picture for
score = 95. Which name points to which object? - If you then write
score = 100, which object doesscorepoint to now? - Challenge: after
a = [1, 2]andb = a, do you have two lists, or two names?
One-Sentence Takeaway
In Python, a variable is not a box that stores data; it is a name label that points to an object.
This idea will make lists, function parameters, and mutable objects much easier to understand later.
Questions or feedback?
Have a question about this article or want to suggest an improvement? Send us an email.
