Card 1 of 10 · Data types
What is the difference between mutable and immutable types?
Say this first
Mutable objects can change in place (list, dict, set). Immutable ones cannot (int, str, tuple, frozenset).
Picture it
Mutable
- • list, dict, set
- • Changed in place, same object
- • Cannot be a dict key (not hashable)
- • Shared references see the change
Immutable
- • int, float, str, tuple, frozenset
- • 'Changing' makes a new object
- • Hashable, so usable as dict keys
- • Safe to share
Try it
Which statement raises a TypeError?
Common trap
Saying a tuple can never change. A tuple holding a list is fixed, but the list inside can still be modified.
Say it like this
Mutable objects change in place, immutable ones create a new object, and only immutable (hashable) objects can be dict keys.