In this lesson you’ll have a look at different examples of mutable and immutable objects in Python. The objects being covered include lists, tuples and dictionaries.
Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Examples for Mutable and Immutable Objects
00:00 Now, what are some examples for mutable objects? A typical mutable object would be a list, like we had before. It could also be a dictionary. We can change those after the fact.
00:13 Or it could also be a set. Those are all mutable. Then if you have a custom class, that’s also mutable by default. We can just go in and change attributes on the class.
00:24 Now, what are some examples for objects that are immutable, that can’t be freely modified in Python? One typical example would be a string.
00:34
The string '123', we can’t actually reach in and modify characters in that string. So it’s immutable. Another example would be the tuple that I just showed you.
00:45
Or another example could be a frozenset. So I’m going to create a frozenset here. And a frozenset, well, it’s frozen, so we can’t freely modify it.
00:54 Those would be examples of immutable objects or immutable values in Python.
Become a Member to join the conversation.