Learning

Tuples & Immutability

Tuples & Immutability

Creating Tuples

python
# Literal syntax (parentheses are optional but recommended) coords = (10, 20) single = (42,) # ⚠️ Comma is REQUIRED for single element! not_tuple = (42) # This is just an int in parentheses empty = () # From other iterables from_list = tuple([1, 2, 3]) # (1, 2, 3) from_str = tuple('Python') # ('P', 'y', 't', 'h', 'o', 'n')

Tuple Operations

python
t = (10, 20, 30, 20, 40) # Indexing & Slicing (same as lists) print(t[0]) # 10 print(t[-1]) # 40 print(t[1:3]) # (20, 30) # Membership print(20 in t) # True # Count & Index print(t.count(20)) # 2 print(t.index(30)) # 2 # Concatenation & Repetition (creates NEW tuples) new_t = t + (50, 60) rep_t = (0,) * 3 # (0, 0, 0)

Immutability

python
t = (1, 2, [3, 4]) # t[0] = 99 # TypeError! 'tuple' object does not support item assignment # ⚠️ BUT if a tuple contains a MUTABLE object, that inner object CAN change! t[2][0] = 99 print(t) # (1, 2, [99, 4]) — Tuple still points to the same list object

Tuple Unpacking

python
# Basic unpacking x, y, z = (10, 20, 30) # Swap variables (Pythonic!) a, b = 10, 20 a, b = b, a # a=20, b=10 # Extended unpacking (* operator) first, *rest = (1, 2, 3, 4, 5) print(first) # 1 print(rest) # [2, 3, 4, 5] (becomes a list!) *middle, last = (1, 2, 3, 4, 5) first, *middle, last = (1, 2, 3, 4, 5) # Nested unpacking (a, b), (c, d) = (1, 2), (3, 4)

Why Tuples?

python
# 1. Dictionary keys (must be hashable/immutable) locations = { (35.68, 139.69): 'Tokyo', (40.71, -74.00): 'New York' } # 2. Function return values (multiple returns are actually tuples) def min_max(lst): return min(lst), max(lst) result = min_max([3, 1, 4, 1, 5]) print(type(result)) # <class 'tuple'> # 3. Slight performance & memory advantage over lists import sys print(sys.getsizeof([1,2,3])) # 88 bytes print(sys.getsizeof((1,2,3))) # 72 bytes
Key Rules
  • •Single element tuple REQUIRES a trailing comma: (42,) — (42) is just an integer
  • •Tuples are immutable, but if they contain mutable objects (like lists), those inner objects CAN be changed
  • •Tuple unpacking is one of Python's most powerful features — use * to capture remaining elements
  • •Tuples can be used as dictionary keys because they are hashable (as long as all their elements are hashable)
  • •When a function returns multiple values, Python packs them into a tuple automatically
Your Task

Create a function `swap_values(a, b)` that returns the swapped values as a tuple. Create a function `get_stats(numbers)` that takes a list and returns a tuple of `(min, max, sum, average)`. Use tuple unpacking to extract and print the results. Finally, demonstrate that a tuple can be a dict key by creating a coordinate mapping.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should define swap_values returning tuple
Should define get_stats function
Should use min, max, sum built-ins
Should use tuple unpacking for results
Should use tuple as dictionary key