Scrollable Nav Bar

How Computers Think About Memory in Python

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.


1. Computers Store Data in Memory

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:

  • Every location in memory has an address.
  • That address is a number.
  • If the computer knows an address, it can jump directly to that location and read what’s there.

This “jump straight to the right place” behavior is one reason computers are so fast.


2. A familiar example – files, file names, and addresses

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:

  • A file name (what you see in a folder)
  • The file content (the actual text inside)

But the computer needs more structure to find that content again.

A simplified model looks like this:

  • The file name appears in a directory (like the icon you click).
  • Along with the file name, the computer stores where the file content begins in memory.
  • That “where” is an address.

So the file name becomes a kind of label that is connected to an address in memory.

What else is stored with the file name?

Usually, the computer also stores extra information such as:

  • File size (for example, 4 KB)

That means when you open the file:

  1. The computer finds the file name in the directory.
  2. It reads the stored address for where the content starts.
  3. It jumps to that address.
  4. It reads the next N bytes (based on the file size).
  5. It loads that data so you can see the content.

3. Pointers: “arrows” to memory locations

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:

  • A pointer is information that says, “The data is over there in memory.”
  • If you have the pointer (address), you can jump straight to the data.

You don’t usually see pointers directly while writing Python, but the idea of “a name pointing to data” is essential.


4. Variables are named references to data

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:

  • A variable name is like a label.
  • That label is connected to a location in memory where the value is stored.

5. Walking through a simple program step by step

Consider this basic Python code:

a = 2
b = 3
c = a + b

Let’s interpret it using the memory model.

Step 1: a = 2

  • The computer stores the value 2 somewhere in memory.
  • It records that the name a should refer to that location.

Step 2: b = 3

  • The value 3 is stored somewhere in memory.
  • The name b is recorded to refer to that location.

Step 3: c = a + b

To compute a + b, the computer must fetch the values:

  1. Look up a → find where it points → fetch the value 2
  2. Look up b → find where it points → fetch the value 3
  3. Add them → 2 + 3 = 5
  4. Store the result 5 somewhere in memory
  5. Record that the name c refers to the stored result

So what looks like a single expression on your screen becomes a sequence of “look up → jump to memory → fetch → compute → store.”


6. Where this mental model really matters: lists and shared references

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:

What does 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.


7. The surprising result: changing one can change both

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.


8. Why you should care

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:

  • assign one variable to another,
  • pass variables into functions,
  • store variables inside other structures,

this mental model becomes extremely useful.

Programming is largely about:

  • putting data into memory,
  • retrieving data from memory,
  • and manipulating that data.

If you understand that variable names are connected to memory locations, many confusing behaviors suddenly become predictable.


9. Quick recap

  • Computers store data in memory.
  • Every place in memory has an address (a number).
  • If the computer knows the address, it can jump directly to that data.
  • A pointer is a helpful way to think about “an address that leads to data.”
  • A variable name is like a label that refers to a location in memory.
  • With lists, b = a can make two names refer to the same data.
  • Modifying that shared data through one name affects what you observe through the other.

10. Practice: check your understanding

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.