Lesson 02
Immutable Types in Python
Master Python's immutable built-in types with visual memory diagrams, id() proofs, and item-assignment rules.
What you'll learn
| Category | Types |
|---|---|
| Numbers | int, float, bool, complex |
| Text & bytes | str, bytes |
| Sequences | tuple, range |
| Special types | frozenset, NoneType |
From the overview, you know immutable objects are replaced, not edited. This lesson walks through every built-in immutable type.
Visual hub of immutable types
Every type around the center cannot be modified in place.
Memory trick: If it is immutable, Python replaces the object instead of editing it.
1. Numeric types
Includes:
intfloatboolcomplex
Key idea: Arithmetic creates a new object.
Visual proof
x = 10
print(id(x))
x = x + 5
print(x)
print(id(x))Example output (your id() values will differ each run):
140245831990112
15
140245831991264
x = x + 5does not modify10; it creates a new integer object.
See: int · float · bool · complex
2. str and bytes
You can read characters by index, but you cannot replace them.
s = "python"
print(s[0]) # p
# s[0] = "P" # TypeErrorbytes behaves the same:
b = b"ABC"
# b[0] = 65 # TypeErrorRule:
strandbytesare read-only sequences.
See: str · bytes · String Handling overview
3. tuple and range
Tuples look like lists — but they are fixed forever.
List vs tuple
t = (10, 20, 30)
print(t[1]) # 20
# t[0] = 99 # TypeErrorRange:
r = range(5)
print(r[2]) # 2
# r[0] = 10 # TypeError
rangegenerates values, but those values cannot be modified.
4. frozenset vs set
This is a favorite interview question.
Visual comparison
| Type | Mutable? |
|---|---|
set |
Yes |
frozenset |
No |
fs = frozenset([1, 2, 3])
# fs.add(4) # AttributeError
s = {1, 2, 3}
s.add(4) # Works
frozensetis simply a locked version ofset.
5. NoneType
None represents the absence of a value.
It is a singleton — only one None object exists in a program.
x = None
y = None
print(x is y)Output:
TrueBoth variables reference the same object.
See: NoneType
Immutable checklist
If a type is immutable, these rules apply.
| Test | Result |
|---|---|
| Item assignment | TypeError |
| In-place mutation | Not available |
id() after modification |
New object |
| Examples | str, tuple, int |
Playground
Run this example and observe the behavior.
x = 10
print("int id before:", id(x))
x = x + 5
print("int id after:", id(x))
t = (1, 2, 3)
# t[0] = 9 # TypeError
fs = frozenset({1, 2, 3})
print(fs)Observe:
- Integer → new ID
- Tuple → cannot modify
- Frozenset → fixed membership
Use the lesson playground at the bottom of the page to run this code live.
Quick knowledge check
-
Is
tuplemutable?
Answer: No — tuples are immutable. -
Can you call
add()on afrozenset?
Answer: No — only a regularsetsupportsadd(). -
Does
x = x + 1modify the original integer?
Answer: No — Python creates a new integer object.
30-second interview revision
| Type | Remember |
|---|---|
int, float |
Arithmetic creates new objects |
str, bytes |
Read by index, cannot assign |
tuple |
Fixed sequence |
range |
Read-only generated values |
frozenset |
Immutable set |
None |
Singleton immutable object |
Interview shortcut: If obj[index] = value raises TypeError, the object is immutable. If the object has methods like add() or append(), it is usually mutable.
Next: Mutable Types in Python.
Loading playground…