Tuples
Store fixed values in tuples and unpack them into useful names.
A tuple is a fixed sequence
A tuple holds ordered values like a list, but you cannot change its items. Tuples usually use parentheses.
For a one-item tuple, the trailing comma is important.
point = (3, 5)
one_item = ('hello',)
print(point[1])
print(one_item)
print(point.count(3))
print(point.index(5))
Tuples support indexing and slicing. Their two common methods are count(), which counts a value, and index(), which finds a value's position.
Tuples cannot be changed
coordinates = (10, 20) coordinates[0] = 99
Unpack a tuple
person = ('Mina', 14, 'Python')
name, age, subject = person
first, *middle, last = (1, 2, 3, 4)
print(name)
print(age)
print(subject)
print(middle)
print(last)
Unpacking puts tuple values into variables. A starred name such as *middle collects any remaining values into a list.
| Choose | When it fits | Example |
|---|---|---|
| Tuple | Values should stay fixed | A coordinate: (x, y) |
| List | You expect to add, remove, or replace items | A shopping list |
Key points
- Tuples are ordered sequences.
- A one-item tuple needs a trailing comma.
- Tuple items cannot be changed.
- Unpacking assigns tuple values to variables.
- Use a tuple for fixed data and a list for changing data.
Worked example
GPS coordinates never change in order — the Sundarbans station as an immutable pair.
station = (21.9497, 89.1833) lat, lon = station print(lat, lon)
How it works, step by step
- (...) makes a tuple: fixed structure, unchangeable contents.
- Unpacking copies both fields into named variables in one line.
- Trying station[0] = 0 would raise TypeError.
References & Further Reading
Related entries from PyLabs's own reference library.
Practice exercise
Not passed yetMake a tuple city = ('Paris', 'France'). Unpack it into name and country, then print name.
Expected output: Paris
# Write your code below
Check yourself
Tuples quiz
10 questions. You get instant feedback per question, and the timer starts when you press the button.
Lesson incomplete
Not completedCreate an ID or sign in to save lesson completion across devices.