# 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.

Subject: Python | Lesson 1 of 24 | 6 min read
Interactive lesson: https://codewithanimation.in/python/variables-and-types
Next: [Operators & Identity](https://codewithanimation.in/python/operators.md)

## 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.

## Common mistake: The Shared Mutable Reference Trap

The most notorious beginner bug occurs when assigning one list to another: `a = [1, 2]`, followed by `b = a`. Calling `b.append(3)` modifies `a` as well, because both variables reference the identical list in memory.

To create an independent copy, use `b = a.copy()` or slice copying `b = a[:]`. For deeply nested objects containing other lists, use `copy.deepcopy()`.

## Tip: Verify with hex(id(obj))

When debugging strange mutation issues, print `hex(id(a))` and `hex(id(b))`. If both hex values match, you are interacting with the exact same object in RAM.

## Try it: 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.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/python/variables-and-types

## Check yourself

### Question 1

What actually happens under the hood when you execute `x = 42` in Python?

- A. Python creates an integer object 42 in heap memory and binds the name tag x to it.
- B. Python reserves a 4-byte box in the stack named x and stores the raw binary number 42 inside.
- C. Python declares x as a global integer variable that cannot point to other types.

Correct answer: A

Python variables are symbolic labels bound to heap-allocated objects. The type information is stored inside the object, not the variable name.

- Why not B: Incorrect. That is how low-level languages like C work. Python variables are references to heap objects.
- Why not C: Incorrect. Python variables are dynamically typed and can be rebound to any object.

### Question 2

If `a = [1, 2]` and `b = a`, what is the result of `b.append(3)` on `a`?

- A. a becomes [1, 2, 3] because both a and b reference the exact same list object in memory.
- B. a remains [1, 2] because b is an independent copy created by assignment.
- C. A TypeError is raised because lists cannot be shared across multiple variables.

Correct answer: A

Assignment `= ` binds a second pointer tag to the existing list. Mutating the list via `b` mutates the single shared instance in memory.

- Why not B: Incorrect. Assignment in Python never copies data; it only binds a second reference.
- Why not C: Incorrect. Multiple variables can reference the same mutable object freely.

### Question 3

Which of the following data types in Python is strictly immutable?

- A. tuple
- B. list
- C. dict

Correct answer: A

Tuples, along with integers, floats, strings, and booleans, are immutable. Once created, their items cannot be modified, added, or removed.

- Why not B: Incorrect. Lists can be modified in place with append(), pop(), and index assignment.
- Why not C: Incorrect. Dictionaries can have keys added, removed, and updated in place.

## 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.
