Tuples
Immutable ordered collections — a list that cannot be changed, and why that is actually a superpower in many situations.
"A list is a whiteboard — you can write, erase, and rewrite. A tuple is a printed certificate — once printed, it is permanent. Both have their place."
— ShurAIList vs Tuple — The Core Difference
Mutable — can change
Add, remove, sort items
Use for: shopping carts, scores, anything that grows or changes
Immutable — cannot change
No add, remove, or sort
Use for: coordinates, fixed data, records that should not change
Creating a Tuple
Use parentheses () with items separated by commas. You can also create one without parentheses — the comma is what actually makes it a tuple:
# A tuple of a person's fixed details
person = ("Riya Sharma", 22, "Mumbai")
# GPS coordinates — should never change
location = (19.0760, 72.8777)
# RGB colour value
red = (255, 0, 0)
# Even without parentheses — the comma makes it a tuple
point = 10, 20
print(type(point)) # <class 'tuple'>
# Single-item tuple — MUST have trailing comma
single = (42,) # tuple
not_tuple = (42) # just the int 42
print(type(single)) # <class 'tuple'>
print(type(not_tuple)) # <class 'int'>
Tuples Cannot Be Changed — Deliberately
If you try to change a tuple item, Python raises a TypeError. This is a feature, not a bug:
coords = (19.07, 72.87)
# OK — reading works fine
print(coords[0]) # 19.07
# ERROR — changing raises TypeError
coords[0] = 18.5
# TypeError: 'tuple' object does not support item assignment
When data should not change — like GPS coordinates, a date of birth, or an RGB colour — using a tuple signals that intent clearly. It protects the data from accidental modification and makes your code safer and slightly faster than a list.
Accessing Items — Same as Lists
Indexing, negative indexing, and slicing all work identically to lists:
person = ("Arjun", 25, "Delhi", "Engineer")
print(person[0]) # Arjun
print(person[2]) # Delhi
print(person[-1]) # Engineer
print(person[1:3]) # (25, 'Delhi')
print(len(person)) # 4
Tuple Unpacking — Python's Most Elegant Trick
You can assign each item in a tuple to separate variables in a single line. This is called unpacking and is used constantly in real Python code:
# Basic unpacking
person = ("Riya", 22, "Mumbai")
name, age, city = person
print(name) # Riya
print(age) # 22
print(city) # Mumbai
# Unpack directly in a for loop — very common pattern
students = [
("Riya", 88),
("Arjun", 75),
("Sneha", 92),
]
for name, score in students: # each tuple is unpacked!
print(f"{name} scored {score}")
Swap Two Variables in One Line
Python uses tuple unpacking to swap variables without any temporary variable — clean and unique to Python:
a = 10
b = 20
# Most languages need a temp variable:
# temp = a; a = b; b = temp
# Python — one clean line
a, b = b, a
print(a) # 20
print(b) # 10
When to Use a Tuple vs a List
| Situation | Use | Why |
|---|---|---|
| Shopping cart items | list | Items get added and removed |
| GPS coordinates (lat, lng) | tuple | A location should not be modified accidentally |
| Student marks in a class | list | New students added, marks updated |
| RGB colour (255, 0, 0) | tuple | A colour definition is fixed |
| Days of the week | tuple | There are always exactly 7 days |
| API search results | list | You will filter, sort, and display them |
Real Example — Train Timetable
Each train record is a tuple (fixed schedule data). We store them in a list because there are many trains:
# (train_name, departure, arrival, platform)
timetable = [
("Rajdhani Express", "06:00", "14:30", 1),
("Shatabdi Express", "07:15", "11:45", 3),
("Duronto Express", "09:30", "18:00", 2),
("Garib Rath Express", "22:00", "08:15", 5),
]
print(f"{'Train':22} {'Dep':>6} {'Arr':>6} Plat")
print("-" * 44)
for train, dep, arr, plat in timetable:
print(f"{train:22} {dep:>6} {arr:>6} {plat}")
print()
search = input("Search train name: ").lower()
for train, dep, arr, plat in timetable:
if search in train.lower():
print(f"\n Train: {train}")
print(f" Departs : {dep}")
print(f" Arrives : {arr}")
print(f" Platform: {plat}")
Train Dep Arr Plat
--------------------------------------------
Rajdhani Express 06:00 14:30 1
Shatabdi Express 07:15 11:45 3
Duronto Express 09:30 18:00 2
Garib Rath Express 22:00 08:15 5
Search train name: shatabdi
Train: Shatabdi Express
Departs : 07:15
Arrives : 11:45
Platform: 3
"Choose a tuple when the data is a fact — not a collection to manage, but a fixed record that describes something. The immutability is a promise."
— ShurAIQuiz — Question 1
How is a tuple different from a list?
Quiz — Question 2
How do you create a tuple with exactly ONE item?
Quiz — Question 3
What is tuple unpacking?
Quiz — Question 4
Which of these is the best use case for a tuple?