Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 02

Immutable Types in Python

Master Python's immutable built-in types with visual memory diagrams, id() proofs, and item-assignment rules.

Beginner20 min

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.

Immutable types hub — LOCK center with numbers str tuple bytes frozenset range None

Memory trick: If it is immutable, Python replaces the object instead of editing it.

1. Numeric types

Includes:

  • int
  • float
  • bool
  • complex

Key idea: Arithmetic creates a new object.

Visual proof

int id=101 value 10 plus 5 becomes id=205 value 15
python
x = 10

print(id(x))

x = x + 5

print(x)
print(id(x))

Example output (your id() values will differ each run):

text
140245831990112
15
140245831991264

x = x + 5 does not modify 10; 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.

python string read by index OK but s[0]=P raises TypeError
python
s = "python"

print(s[0])      # p

# s[0] = "P"     # TypeError

bytes behaves the same:

python
b = b"ABC"

# b[0] = 65      # TypeError

Rule: str and bytes are 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

List lst[0]=99 works vs Tuple t[0]=99 TypeError
python
t = (10, 20, 30)

print(t[1])      # 20

# t[0] = 99      # TypeError

Range:

python
r = range(5)

print(r[2])      # 2

# r[0] = 10      # TypeError

range generates values, but those values cannot be modified.

See: tuple · range

4. frozenset vs set

This is a favorite interview question.

Visual comparison

set supports add/remove vs frozenset locked
Type Mutable?
set Yes
frozenset No
python
fs = frozenset([1, 2, 3])

# fs.add(4)      # AttributeError

s = {1, 2, 3}

s.add(4)         # Works

frozenset is simply a locked version of set.

See: frozenset · set

5. NoneType

None represents the absence of a value.

It is a singleton — only one None object exists in a program.

x and y both point to the same None singleton object
python
x = None
y = None

print(x is y)

Output:

text
True

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

python
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

  1. Is tuple mutable?
    Answer: No — tuples are immutable.

  2. Can you call add() on a frozenset?
    Answer: No — only a regular set supports add().

  3. Does x = x + 1 modify the original integer?
    Answer: No — Python creates a new integer object.

30-second interview revision

Immutable types hub — int float str bytes tuple range frozenset
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…