Data Structures14 min read

Tuple Essentials

Understand tuples clearly: why immutability matters, where tuples are better than lists, and how packing/unpacking works with real examples.

David Miller
December 21, 2025
0.0k0

A **tuple** is like a list, but **immutable** (cannot be changed). ### When to use tuples Use tuples when: - data should not change (like coordinates) - you want safer code (no accidental edits) - you want to return multiple values from a function ## Create tuples ```python point = (10, 20) user = ("Tom", 25, "Austin") ``` ## Single-item tuple (common beginner confusion) ```python x = (5) # this is int, not tuple y = (5,) # this is tuple ``` ## Access items ```python point = (10, 20) print(point[0]) # 10 ``` ## Tuple unpacking (very important) ```python person = ("Sarah", 28) name, age = person print(name, age) ``` ## Why immutability is useful If you store config settings, you don’t want changes by mistake. ```python CONFIG = ("localhost", 5432) # CONFIG[0] = "prod" # error, cannot change ``` ## Graph: list vs tuple ```mermaid flowchart LR A[List] --> B[Mutable: can change] C[Tuple] --> D[Immutable: cannot change] ``` ## Remember - Tuples are ordered but immutable - Great for fixed data - Unpacking is a powerful Python skill

#Python#Beginner#Tuple