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

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.

Beginner18 min

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:

Mutable same id vs Immutable new id 101 to 205

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.

Same memory address id=5001 before and after list edit
python
lst = [10, 20, 30]

print(id(lst))

lst[0] = 99

print(lst)
print(id(lst))

Expected pattern (your exact numbers will differ):

text
4362815488        # example id

[99, 20, 30]

4362815488        # same id — list was modified in place

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

python id=101 becomes PYTHON id=205
python
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)

List lst[0]=99 works
python
lst = [10, 20, 30]

lst[0] = 99      # Works

Immutable (not allowed)

String s[0]=P raises TypeError
python
s = "python"

# s[0] = "P"     # TypeError

Rule: 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:

Mutable 5001→5001 vs Immutable 101→205

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: set is mutable, but frozenset is immutable.

Deep dives: Immutable types · Mutable types

6. id() Demonstration

The id() function returns the identity (memory reference) of an object.

python
s = "python"

print("Before:", id(s))

s = s.upper()

print("After :", id(s))

Conceptual pattern:

text
Before: 10123456
After : 10198765    # different — new object
Before python id A → After PYTHON id B

Different id() values mean Python created a new object. Related: is / is not.

Playground

Run this complete example to compare both behaviors.

python
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 changes
  • list → ID stays the same

Use the lesson playground at the bottom of the page to run this code live.

Quick knowledge check

  1. Can you run s[0] = "P" on a string?
    Answer: No — strings are immutable and raise TypeError.

  2. Does lst[0] = 99 create a new list?
    Answer: No — it modifies the existing list.

  3. Which function shows an object's identity?
    Answer: id(obj)

30-second interview revision

Object hub — Mutable same ID vs Immutable new ID
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…