In Python, immutable means you are not able to edit something. Mutable means you are. This is due to the way the data types are created in Python. Don’t fear, this isn’t a blocker, there is always a solution if you run into a problem.
The common types of immutable data are int, float, bool, str, tuple.
my_string = "Data" my_string[2] = "T >>> TypeError: 'str' object does not support item assignment
Python Immutable
If you run into an immutable object, and you need to edit it, then you’ll need to change the data type. For example: change your tuple to a list, or dict.
Let’s look at a quick code sample
Python Immutable¶
In python, immutable means you can't edit or change something. Mutable means you can. For example, you can't assign values in a tuple, but you can in a list. Let's take a look
Mutable Objects¶
my_list = [1,2,3,4,"Bob"]
my_list
my_list[0] = "New first position"
my_list
my_dictionary = {"City1" : "San Francisco", "City2" : "Los Angeles"}
my_dictionary
my_dictionary["City1"] = "Austin"
my_dictionary
Immutable Objects¶
my_tuple = (1,2,3)
my_tuple
my_tuple[1] = 100
my_string = "My String"
my_string[2] = "T"
my_set = set([1,2,3,4])
my_set.add(5)
my_set
my_set[2] = "Ted"
my_frozenset = frozenset([1,2,3,4])
my_frozenset.add(10)
my_frozenset
Check out more Python Vocabulary on our Glossary Page