Python Variables Demystified: What's in a Name?

 

Python Variables Demystified: What's in a Name?





If you've been using Python for a while, you know how to assign a variable. It’s one of the first things we learn: x = 10. But have you ever stopped to ask what really happens when you run that line?

For intermediate and senior developers, understanding this mechanism is the master key that unlocks Python's behavior around mutability, memory management, and function arguments. So, let's peel back the first layer. 🧅

Variables are Names, Not Boxes

The most common mental model—a variable as a box holding a value—can lead to confusion in Python. A more accurate analogy is that of a name tag or sticky note.

In Python, a variable is a name (or label) that is bound to an object living somewhere in your computer's memory.

When you write:

my_var = "Hello, World!"

You are not putting the string into a box called my_var. You are creating the string object "Hello, World!" in memory and then attaching the name tag my_var to it.

Identity: The Object's True Self

Every object in Python has a unique identity, a sort of permanent memory address. You can find it using the id() function. This identity is what the is keyword checks.

a = [1, 2, 3]  # Create a list object, attach the name `a` to it.
b = a          # Attach a new name `b` to the SAME object.

print(id(a))   # Output: e.g., 140247582398784 (this will change each run)
print(id(b))   # Output: 140247582398784 (Same as id(a)!)
print(a is b)  # Output: True. They are the same object.

This shows that we have one list object with two different names. An action performed using one name will be visible through the other.

a.append(4)
print(b)  # Output: [1, 2, 3, 4]
# Changing 'a' changed 'b' because they reference the same object.

Assignment vs. Equality: is vs. ==

This brings us to a critical distinction:

  • a is b checks if two names point to the exact same object (identity check).
  • a == b checks if the objects the names point to have the same value (value check).
list1 = [1, 2, 3]
list2 = [1, 2, 3]  # A new, separate list object with the same value.

print(list1 == list2)  # True? Yes, their values are equal.
print(list1 is list2)  # True? No! They are two different objects.
print(id(list1), id(list2)) # Their identities (memory addresses) are different.

What Happens During Assignment?

Let's trace through the process. The key is to think of the assignment operator = as the "sticky note applicator."

x = 1000
  1. The interpreter creates an integer object 1000 in memory.
  2. It applies a sticky note labeled x to this object.

Now, what happens when we do:

y = x
  1. It does not create a new copy of the object 1000.
  2. It simply applies a new sticky note labeled y to the exact same object that the x sticky note is on.

We can visualize this relationship. Both names point to the same underlying object.

    (Memory)
    ┌─────────────┐
    │ int object  │
    │   value:1000│ <---(name tag "x")
    └─────────────┘ <---(name tag "y")

Key Takeaway 🗝️

The fundamental concept to internalize is this: In Python, variables are references to objects, not containers for values.

This "names and bindings" model is the cornerstone of understanding Python. It explains why appending to a list passed into a function changes the original, and it's the reason for the different behavior you'll see in this challenge.

Speaking of which, test your understanding with this snippet! Based on what we've learned, what will the final output be? 💭

a = 500
b = a
a = a + 100

list1 = [1, 2]
list2 = list1
list1.append(3)

print(f"b is {b}")
print(f"list2 is {list2}")

Hint: The behavior is different for integers and lists. Why might that be? We'll answer that exact question in our next post on mutability and Immutability.


Aaron Rose is a software engineer and technology writer at tech-reader.blog and the author of Think Like Genius.

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Insight: The Great Minimal OS Showdown—DietPi vs Raspberry Pi OS Lite

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison