Andromeda
Note

Tuples (Python)

Definition

An immutable collection of items in a particular order. Once a tuple is defined, the items within it cannot be changed, added, or removed.

Why It Matters

Tuples provide ‘immutable’ data structures, ensuring that a collection of values cannot be accidentally changed by other parts of the program. They are the ‘safe-deposit boxes’ of Python, essential for data integrity and efficient memory usage.

Core Concepts

  • Syntax: Defined using parentheses () instead of square brackets.
  • Immutability: Attempting to change an element results in a TypeError.
  • Reassignment: While elements cannot change, the variable holding the tuple can be reassigned to a completely new tuple.
  • Structure: Best used for small collections of data that should remain constant throughout the program’s life.
dimensions = (800, 600)
print(dimensions[0])

# Attempting to change an element would raise a TypeError:
# dimensions[0] = 1000

# Reassigning the variable is allowed:
dimensions = (1024, 768)

Connected Concepts