Course contentsShow
← Python
Lesson 20 of 1,0601. Foundations and EnvironmentFree lesson

Variables as Names for Objects

You will learn that Python variables are labels pointing to objects in memory rather than storage containers for data values.

In this lesson, you will learn that in Python, a variable is not a box that holds a value, but rather a name tag pointing to an object living in your computer's memory.

Beyond the "Container" Analogy

In many introductory programming courses, variables are described as "boxes" where you store data. While this is a helpful way to visualize assignment, it isn't quite how Python works under the hood. In Python, everything is an object. When you write code like x = 5, Python creates an integer object representing the number 5 in memory, and the variable x simply acts as a label or a reference pointing to that specific object.

Labels in Action

Consider this scenario:

a = [1, 2, 3]
b = a

In the example above, you haven't created two separate lists. You have created one list object in memory, and now both a and b are pointing to that exact same object. If you were to modify the list using a, the changes would be reflected when you look at b, because they are both just different names for the same thing.

Think of it like a sticky note. If you have an object (like a list or a number) sitting on your desk, and you put a sticky note labeled a on it, you are identifying that object. If you then put another sticky note labeled b on the same object, you haven't duplicated the object; you’ve simply added a second name to reference it.

Why this matters

Understanding this distinction is critical for how Python manages memory. Python automatically keeps track of how many names are pointing to an object. When a variable name is reassigned, or when it goes out of scope, the "link" is broken. If no names are pointing to an object anymore, Python’s memory management system knows that object is no longer needed and can clean it up. By thinking of variables as names, you stop viewing memory as a fixed grid of boxes and start seeing it as a dynamic collection of objects managed by references.

Key Takeaway: A variable is not a container for data, but a name pointing to an object in your computer's memory.