When we write Python code, it can feel like we’re just manipulating names and values on the screen. But under the hood, the computer is doing something very physical: it is storing data in memory and jumping to locations in memory to retrieve that data.
You don’t need to think about this for every line you write. Still, having a clear mental model of how data lives in memory and how variable names connect to that data will save you from confusion later—especially when you work with collections like lists.
Imagine your computer’s memory as a huge grid of storage slots. Each slot is a place where a piece of data can live. The important part is this:
This “jump straight to the right place” behavior is one reason computers are so fast.
You already understand this idea from saving files.
When you save a file on a computer (for example, a text file), you typically think of it as:
But the computer needs more structure to find that content again.
A simplified model looks like this:
So the file name becomes a kind of label that is connected to an address in memory.
Usually, the computer also stores extra information such as:
That means when you open the file:
An address is just a number, but conceptually it behaves like an arrow that tells the computer where to go.
We often represent this idea as a pointer:
You don’t usually see pointers directly while writing Python, but the idea of “a name pointing to data” is essential.
When you run a program, you’re constantly creating and using small pieces of data—like mini “stored items” inside memory.
In programming, we give names to these stored items so we can use them again.
Those named pieces are called variables.
A helpful mental model is:
Consider this basic Python code:
a = 2
b = 3
c = a + b
Let’s interpret it using the memory model.
a = 22 somewhere in memory.a should refer to that location.b = 33 is stored somewhere in memory.b is recorded to refer to that location.c = a + bTo compute a + b, the computer must fetch the values:
a → find where it points → fetch the value 2b → find where it points → fetch the value 32 + 3 = 55 somewhere in memoryc refers to the stored resultSo what looks like a single expression on your screen becomes a sequence of “look up → jump to memory → fetch → compute → store.”
Now consider a slightly different example:
a = [1, 2, 3, 4, 5]
b = a
In Python, square brackets [...] mean you are creating a list—a collection of values.
Here’s the key moment:
b = a actually do?It does not create a brand-new, separate copy of the list.
Instead:
a refers to a list stored somewhere in memory.b = a makes b refer to the same location in memory.In other words, a and b become two names for the same list.
Now imagine you modify the list using a:
a[0] = 7
You changed the first element of the list. But because a and b refer to the same list in memory, the visible effect shows up through both names.
So if you check:
a now represents [7, 2, 3, 4, 5]b also represents [7, 2, 3, 4, 5]This is not because Python is “magical” or confusing. It’s because both names point to the same data behind the scenes.
For simple programs with individual values, you can often ignore the memory details and still write correct code.
But as soon as you work with collections—especially when you:
this mental model becomes extremely useful.
Programming is largely about:
If you understand that variable names are connected to memory locations, many confusing behaviors suddenly become predictable.
b = a can make two names refer to the same data.Try this small experiment in Python:
a = [1, 2, 3]
b = a
a[1] = 99
print(a)
print(b)
Code language: PHP (php)
Before you run it, predict the output.
If your prediction matches what happens, your mental model is already improving—which will help a lot as you go deeper into Python programming.