Junior — Middle
What types of data structures support modification of their elements, and which do not?
sobes.tech AI
Answer from AI
In Python, data structures are divided into mutable and immutable.
Mutable data structures:
- list
- dict
- set
Immutable data structures:
- tuple
- str
- frozenset
For example, a list can be modified by adding or removing elements:
my_list = [1, 2, 3]
my_list.append(4) # Now my_list = [1, 2, 3, 4]
But a tuple cannot be changed after creation:
my_tuple = (1, 2, 3)
# my_tuple[0] = 10 # This will raise a TypeError