Middle
What are the variable types in Python? Mutable and immutable?
sobes.tech AI
Answer from AI
In Python, variables can be divided into mutable and immutable types.
Immutable types:
int,float,complexboolstr(strings)tuplefrozensetbytes
Objects of these types cannot be changed after creation — any operations create a new object.
Mutable types:
listdictsetbytearray
These objects can be modified "in place" — adding, removing, or changing elements.
Example:
# Immutable type
s = "hello"
s2 = s.replace('h', 'H') # a new string is created
# Mutable type
lst = [1, 2, 3]
lst.append(4) # the list is modified in place
Understanding mutability is important for working with functions, copying objects, and managing state.