What are mutable and immutable types in Python?

A mutable object can be changed after it is created, and an immutable object cannot. When you “change” an immutable value, Python actually creates a new object.

MutableImmutable
list, dict, setint, float, bool, str, tuple, frozenset
numbers = [1, 2]
numbers.append(3)
print(numbers)

text = "hi"
print(text.upper())
print(text)

text[0] = "H"

Output:

Tutorials Class - Output Window

[1, 2, 3]
HI
hi
TypeError: 'str' object does not support item assignment

The list was changed in place, but the string stayed the same, and upper() gave a new string. This matters when you pass a list to a function, because the function can change the original list. See Python Data Types.


Learn more about the similar topics:
Tutorials
Python Lists
Python Tuples
Python Sets
Python Dictionary
Exercises & Assignments
Python program to find largest and smallest number in a list
Python program to remove duplicates from a list
Interview Questions & Answers
What is a list in Python?
What is a tuple in Python?
What is a set in Python?
What is a dictionary in Python?
What is a negative index in Python and why is it used?
What is the difference between a list and a tuple in Python?
What are mutable and immutable types in Python?