Junior — Middle
Can you list data types in Python that cannot be modified after creation?
sobes.tech AI
Answer from AI
Python has several immutable data types, which cannot be changed after creation:
- int — integers
- float — floating-point numbers
- bool — boolean values (True, False)
- str — strings
- tuple — tuples
- frozenset — immutable set
For example, if you have a string, you cannot change a single character; you can only create a new string:
s = "hello"
# s[0] = 'H' # Error
s = "H" + s[1:] # Create a new string
Unlike these, lists (list), sets (set), dictionaries (dict) are mutable types.