SkillByAIOpen interactive version →

Lesson 11 / 49

Tuples

Group fixed values with immutable tuples, unpack them and avoid the one-item tuple comma trap.

Immutable by design

A tuple looks like a list but can't be changed after creation. Use it for data that shouldn't move.

point = (10, 20)
name, age = ("Ada", 36)   # unpacking
print(point[0])           # 10
# point[0] = 99           # TypeError

The trailing comma trap

A one-item tuple needs a trailing comma: single = (5,) — without it, (5) is just the int 5.