Series: Programming Concepts in Plain English
Part 1: Learning Programming Alone: From Panic Loops to Mental Pictures
When I first started learning programming, someone asked me a question I never forgot:
"What is the difference between a variable and a string?"
At that time I didn't even fully understand what a variable was. But the question was really testing whether I understood the concept of a variable. That moment forced me to rethink everything I assumed.
Most beginners imagine a variable as a box that stores data. In many programming languages, that idea works well enough.
But in languages like Python, a variable behaves differently. It is better understood as a label attached to an object.
Let's explore both ideas.
The Box Model (common beginner model)
Imagine you work in a warehouse and must store different products.
- Each product is placed in a carton.
- The carton holds the product.
- The product inside can change without replacing the carton itself.
This is the "box" idea:
- The variable is the carton (the container).
- The data is what you put inside the carton.
- You can replace the contents without replacing the container.
Example in JavaScript:
let blueCarton = "10 balls"
Here:
-
blueCartonis the carton -
"10 balls"is the product inside the carton
If the contents change, we keep the same carton but replace what is inside:
blueCarton = "5 oranges"
Now the carton still exists, but it holds something different. The container stayed the same. Only the content changed.
This model is simple and useful for beginners.
However, Python does not treat variables like cartons that store data. Instead, Python treats variables like labels attached to objects.
The Label Model (how Python actually works)
Python does not store data inside variables. Instead, Python stores objects separately, and variables are simply names that refer to them.
Think of a supermarket shelf:
- The product exists independently.
- A price tag is attached to the product.
- The tag does not contain the product.
- The tag only identifies which product we mean.
That is how Python variables behave.
A variable is a name attached to an object.
Python Example
cosmetic = "perfume"
skin_deep = cosmetic
What happens conceptually:
- The string
"perfume"exists as an object. - The name
cosmeticrefers to that object. - The name
skin_deepis attached to the same object.
There is one object, but two names pointing to it. It is like placing two labels on the same product. People can refer to it by either name, but it is still one product.
If we later write:
cosmetic = "lotion"
Now:
-
cosmeticrefers to"lotion" -
skin_deepstill refers to"perfume"
The object didn't move. The name moved.
It is like peeling the label cosmetic off the perfume and sticking it onto a lotion bottle. The perfume still exists, but now it only has the label skin_deep.
The Key Idea
A simple rule explains everything:
In Python, a variable is a name that refers to an object.
Assignment attaches a name to an object.
Reassignment moves the name.
What's Next?
This is part of my Programming Concepts in Plain English series, where I break down programming ideas using concrete, real-world examples. Check out Part 1: Learning Programming Alone: From Panic Loops to Mental Pictures to see how this series started!
What programming concept confused you the most when you started? Drop a comment below! π
Top comments (0)