Sobes.tech
Middle

Which data types are mutable and which are immutable?

sobes.tech AI

Answer from AI

In Python, data types are divided into mutable and immutable.

Immutable types:

  • int
  • float
  • bool
  • str
  • tuple
  • frozenset

These objects cannot be changed after creation. Any operation that appears to modify them actually creates a new object.

Mutable types:

  • list
  • dict
  • set
  • bytearray

These objects can be modified "in place" — adding, removing, or changing elements without creating a new object.

Example:

x = [1, 2, 3]  # mutable list
x.append(4)    # list has changed

s = "abc"     # immutable string
s = s + "d"   # a new string is created
Which data types are mutable and which are immutable… - sobes.tech