Hackforge Academy

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)

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community