What is the difference between a mutable and an immutable data type in Python?

84 views

What is the difference between a mutable and an immutable data type in Python?

What is the difference between a mutable and an immutable data type in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0
  • Mutable: Objects whose values can be changed after creation (e.g., listdictset).
  • Immutable: Objects whose values cannot be changed after creation (e.g., intfloatstringtuple).

Example:

# Mutable (list)
my_list = [1, 2, 3]
my_list[0] = 100  # Can modify the list

# Immutable (tuple)
my_tuple = (1, 2, 3)
my_tuple[0] = 100  # Raises TypeError

solveurit24@gmail.com Changed status to publish February 13, 2025
0