Lesson 01
Mutable vs Immutable Objects — Overview
Understand one of Python's most important concepts: which objects can change in place, which create new objects, and how id() reveals the difference.
What you'll learn
| Topic | Focus |
|---|---|
| Mutable objects | Edit data in place |
| Immutable objects | Create a new object |
| Item assignment | Why some types raise TypeError |
id() |
Identify an object's memory identity |
After String Handling, you already saw that strings are read-only. This lesson generalizes that idea to all Python objects.
Visual roadmap
The easiest way to remember the difference:
Memory rule: Mutable = modify · Immutable = replace
1. What is a Mutable Object?
A mutable object allows its content to change without creating a new object.
lst = [10, 20, 30]
print(id(lst))
lst[0] = 99
print(lst)
print(id(lst))Expected pattern (your exact numbers will differ):
4362815488 # example id
[99, 20, 30]
4362815488 # same id — list was modified in placeNotice id() stays the same because the list itself was modified.
2. What is an Immutable Object?
An immutable object cannot change its content. Any modification creates a new object.
s = "python"
print(id(s))
s = s.upper()
print(s)
print(id(s))The variable name s is reused, but it now points to a different object.
3. Item Assignment Rule
A quick way to identify mutability is to ask: can I do obj[index] = value?
Mutable (allowed)
lst = [10, 20, 30]
lst[0] = 99 # WorksImmutable (not allowed)
s = "python"
# s[0] = "P" # TypeErrorRule: Mutable types support item assignment. Immutable types do not.
4. Mutable vs Immutable Comparison
| Feature | Mutable | Immutable |
|---|---|---|
| Can modify content? | Yes | No |
| Memory address changes? | Usually no | Yes (new object) |
| Item assignment | Allowed | TypeError |
| Example | list |
str |
Visual memory:
5. Full Python Type Classification
Mutable types
These objects can change after creation.
| Type | Role |
|---|---|
list |
Ordered collection |
dict |
Key-value mapping |
set |
Unique unordered values |
bytearray |
Editable bytes |
Immutable types
These cannot change after creation.
| Type | Role |
|---|---|
int, float, bool, complex |
Numeric values |
str, bytes |
Text and raw bytes |
tuple, range |
Fixed sequences |
frozenset, NoneType |
Fixed set / sentinel |
Common interview question:
setis mutable, butfrozensetis immutable.
Deep dives: Immutable types · Mutable types
6. id() Demonstration
The id() function returns the identity (memory reference) of an object.
s = "python"
print("Before:", id(s))
s = s.upper()
print("After :", id(s))Conceptual pattern:
Before: 10123456
After : 10198765 # different — new objectDifferent id() values mean Python created a new object. Related: is / is not.
Playground
Run this complete example to compare both behaviors.
s = "python"
print("str id:", id(s))
s = s.upper()
print("after upper id:", id(s))
lst = [10, 20, 30]
print("list id before:", id(lst))
lst[0] = 99
print("list id after:", id(lst))Observe:
str→ ID changeslist→ ID stays the same
Use the lesson playground at the bottom of the page to run this code live.
Quick knowledge check
-
Can you run
s[0] = "P"on a string?
Answer: No — strings are immutable and raiseTypeError. -
Does
lst[0] = 99create a new list?
Answer: No — it modifies the existing list. -
Which function shows an object's identity?
Answer:id(obj)
30-second interview revision
| Remember | Meaning |
|---|---|
| Mutable | Content changes at the same id() |
| Immutable | New object is created |
list |
Mutable |
str |
Immutable |
id(obj) |
Shows object identity |
Interview shortcut: If obj[i] = value works, the object is usually mutable. If it raises TypeError, it is immutable.
Next: Immutable Types in Python.
Loading playground…