Junior — Middle
Can you list immutable data types used in Python?
sobes.tech AI
Answer from AI
In Python, immutable (unchangeable) data types are objects whose state cannot be modified after creation. The main immutable types include:
- int — integers
- float — floating-point numbers
- complex — complex numbers
- bool — boolean values (True, False)
- str — strings
- tuple — tuples
- frozenset — immutable set
- bytes — immutable sequence of bytes
Example:
x = (1, 2, 3)
# Cannot change an element of the tuple
# x[0] = 10 # will raise an error
s = "hello"
# s[0] = 'H' # also an error
fset = frozenset([1, 2, 3])
# fset.add(4) # error, because frozenset is immutable
Immutability is useful for using objects as dictionary keys, set elements, and for data security.