Sobes.tech
Senior

Name mutable and immutable data types in Python.

sobes.tech AI

Answer from AI

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

Immutable types:

  • int (integers)
  • float (floating-point numbers)
  • bool (boolean values)
  • str (strings)
  • tuple (tuples)
  • frozenset (immutable set)

Mutable types:

  • list (lists)
  • dict (dictionaries)
  • set (sets)
  • user-defined objects (if their attributes can be changed)

Example:

# Mutable list
lst = [1, 2, 3]
lst[0] = 10  # allowed

# Immutable tuple
tpl = (1, 2, 3)
# tpl[0] = 10  # will raise TypeError
Name mutable and immutable data types in Python. — QA… - sobes.tech