Sobes.tech
Junior — Middle

Explain the concept of immutability of data in programming.

sobes.tech AI

Answer from AI

Immutability means that once an object is created, its state cannot be changed. In Python, examples of immutable types include strings (str), tuples (tuple), and numbers (int, float). This is useful for preventing errors related to data modification in different parts of a program and facilitates parallel programming.

Example:

s = "hello"
s = s.upper()  # A new string is created, the original remains unchanged
print(s)  # HELLO

Here, the upper() method does not modify the original string but returns a new one. Using immutable objects increases the reliability and predictability of the code.