Sobes.tech
Senior

Name mutable and immutable data types in Python

sobes.tech AI

Answer from AI

In Python, mutable and immutable data types differ in whether the object can be changed after its creation.

Immutable types:

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

Mutable types:

  • list
  • dict
  • set
  • bytearray

For example, a string (str) is immutable — any operation creates a new object, whereas a list (list) can be modified in place:

s = "hello"
s = s.replace('h', 'H')  # a new string is created

lst = [1, 2, 3]
lst.append(4)  # the list is modified in place
Name mutable and immutable data types in Python — QA… - sobes.tech