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.
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. 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.
'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.
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.