Sobes.tech
Middle

What are the variable types in Python? Mutable and immutable?

sobes.tech AI

Answer from AI

In Python, variables can be divided into mutable and immutable types.

Immutable types:

  • int, float, complex
  • bool
  • str (strings)
  • tuple
  • frozenset
  • bytes

Objects of these types cannot be changed after creation — any operations create a new object.

Mutable types:

  • list
  • dict
  • set
  • bytearray

These objects can be modified "in place" — adding, removing, or changing elements.

Example:

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

# Mutable type
lst = [1, 2, 3]
lst.append(4)  # the list is modified in place

Understanding mutability is important for working with functions, copying objects, and managing state.

What are the variable types in Python? Mutable and… - sobes.tech