Category: python
Understanding Mutability
Published on 27 Oct 2026
Explanation
Mutable objects can change in place; immutable objects cannot. Lists, dictionaries, and sets are mutable, while strings and integers are immutable.
Code:
items = [1, 2] items.append(3) text = 'Python' text += '!' print(items, text)
Explanation
Names that reference the same mutable object observe mutations made through either name.
Code:
first = [10, 20] second = first first.append(30) print(second) print(first is second)
Explanation
Operations on immutable values produce a value that can be assigned to a name; they do not mutate the original value in place.
Code:
name = 'Python' name = name + ' Programming' print(name)
Explanation
A tuple's element references cannot be reassigned, but a mutable object contained in it can still be modified.
Code:
data = ('Python', [1, 2])
data[1].append(3)
print(data)
Explanation
Use mutable collections for changing data and immutable values when stable values are useful. Dictionary keys must be hashable.
Code:
student = {'name': 'Arun', 'skills': ['Python']}
student['skills'].append('SQL')
print(student)