Junior — Middle
What data types in Python cannot be changed after creation?
sobes.tech AI
Answer from AI
In Python, immutable data types are those whose values cannot be changed after creation. The main ones include:
- int — integers
- float — floating-point numbers
- bool — boolean values
- str — strings
- tuple — tuples
- frozenset — immutable set
For example, if you have a string, you cannot change a single character; you need to create a new string:
s = "hello"
# s[0] = 'H' # Error
s = "H" + s[1:] # Create a new string
Unlike them, lists (list), sets (set), and dictionaries (dict) are mutable data types.