Junior — Middle
Are data types of containers mutable or immutable in their structure?
sobes.tech AI
Answer from AI
In Python, container data types are divided into mutable and immutable.
-
Mutable containers (for example, lists
list, setsset, dictionariesdict) allow modifying their contents after creation: adding, removing, or changing elements. -
Immutable containers (for example, tuples
tuple, stringsstr) have a fixed structure and content after creation — any operations that seem to modify them actually create a new object.
Example of a mutable container:
lst = [1, 2, 3]
lst.append(4) # the list has changed
Example of an immutable container:
t = (1, 2, 3)
# t[0] = 10 # will cause an error, tuples cannot be changed
Thus, the structure of mutable containers is modifiable, while immutable ones are fixed.