Learning navigation

Code with Animation

Variables & Dynamic Typing

Understand how Python binds names to objects in heap memory, why types belong to values rather than variables, and the crucial difference between mutable and immutable data.

Python6 min readConcept 1 of 24

Names Bound to Objects

In many compiled languages like C or Java, a variable is a named storage box with a fixed type and a fixed memory address where raw bytes reside.

In Python, variables are pointer tags attached to objects allocated in heap memory. When you write x = 42, Python creates an integer object 42 in heap memory and binds the name tag x to it.

Python is dynamically typed: the variable name has no type at all. The object it references holds both the type metadata and the actual value.

Reference Assignment & Immutability

When you execute b = a, Python does not duplicate the underlying object. It simply creates a second name tag b and attaches it to the exact same object in memory.

Basic primitive types in Python - integers (int), floating-point numbers (float), booleans (bool), strings (str), and tuples (tuple) - are strictly immutable. Once created in memory, their contents can never be altered.

When you reassign an immutable variable with a = a + 1, Python allocates a new integer object and moves the tag a to point to it. Any other variable still pointing to the original object remains completely unaffected.

Inspecting Types and Memory Addresses

You can inspect the runtime type of any object using the built-in type() function: type(42) returns <class 'int'>.

To check whether two variables point to the exact same memory allocation, inspect their unique integer address using id(obj) or the identity operator is.

Collections like lists (list), dictionaries (dict), and sets (set) are mutable. If two variables reference the same mutable collection, updating the collection through one variable modifies it for both.

The Memory Tag Lab

Interact with variables a and b. Switch between immutable integers and mutable lists to watch how tags rebind or share heap capsules in real time.

# 1. Bind to int(5)
a = 5
b = a
# Both point to 0x7fa5:
a == b # True (equal)
a is b # True (same id)
Variable TagsHeap Memory Objectsatag 'a'0x7fa5btag 'b'0x7fa5int object @ 0x7fa55refcount: 2 (tags a + b)
★

Python Rule: Assignment (=) never copies memory. It merely attaches a new name tag to an existing heap object.

Variables in Python are named reference tags bound to heap objects, not storage boxes.

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1What actually happens under the hood when you execute `x = 42` in Python?
  2. 2If `a = [1, 2]` and `b = a`, what is the result of `b.append(3)` on `a`?
  3. 3Which of the following data types in Python is strictly immutable?

Remember this

  • Python variables are pointer tags bound to heap objects, not storage boxes.
  • Types belong to the objects themselves, not to the variable names.
  • Primitive types (int, float, bool, str, tuple) are strictly immutable.
  • Assignment (b = a) creates a new reference to the same object, never a copy.
  • Use a.copy() or copy.deepcopy() when an independent mutable duplicate is needed.

Done with this concept?

Mark it complete to track your progress. No login needed.