Chapter 07
Python Tuples
2 topics · 1 sections · beginner-friendly study notes · Updated
A tuple is an ordered, immutable collection.
You will learn how to create tuples, pack and unpack them, slice them, and use their two methods count() and index().
Tuples protect data from accidental changes and are used for dictionary keys, function returns and fixed records.
Coordinates, RGB colours, database rows, returning multiple values from a function.
Python Tuples
A tuple is an ordered, immutable sequence of items, usually written inside round brackets like (1, 2, 3). Once created, its items cannot be added, removed or replaced.
In short: A tuple is a list that is locked. You can read it but not edit it.
Think about it
Immutability gives safety, slightly better speed, and allows tuples to be dictionary keys. A GPS location (latitude, longitude) should never be changed accidentally.
The comma, not the parentheses, creates a tuple: t = 1, 2, 3 works, and a single item needs (5,).
Tuples support indexing, negative indexing, slicing, len, in, + and *, all returning new tuples.
Packing means putting values into a tuple, and unpacking assigns its items to variables: x, y = (3, 4).
The number of variables must match unless you use a star: first, *rest = (1, 2, 3).
Swapping uses tuples: a, b = b, a.
A function that does return a, b actually returns one tuple.
Because tuples are immutable they are hashable (if every item is hashable), so they can be used as dict keys.
Immutability is shallow: a mutable object inside (like a list) can still change.
To "modify" a tuple, convert to a list, edit and convert back.
Key points
Example
CodeTuple creation, packing and unpacking:
t = (10, 20, 30, 40)
print(t[0], t[-1], t[1:3]) # 10 40 (20, 30)
single = (5,)
not_tuple = (5)
print(type(single), type(not_tuple)) # tuple int
packed = 1, 2, 3 # packing without brackets
a, b, c = packed # unpacking
print(a, b, c) # 1 2 3
first, *rest = (1, 2, 3, 4)
print(first, rest) # 1 [2, 3, 4]
x, y = 3, 4
x, y = y, x # swap using tuples
print(x, y) # 4 3
def min_max(nums):
return min(nums), max(nums) # returns one tuple
lo, hi = min_max([4, 9, 1])
print(lo, hi) # 1 9
print((1, 2) + (3,), (0,) * 3) # (1, 2, 3) (0, 0, 0)
lst = list(t) # to edit, convert to list and back
lst[0] = 99
t = tuple(lst)
print(t) # (99, 20, 30, 40)
for item in t: # tuples are iterable
print(item)Python Tuple Methods
A tuple has only two built-in methods: count(x) returns how many times x occurs and index(x) returns the position of the first x. Common built-in functions such as len, min, max, sum, sorted and tuple also work with tuples.
In short: Because tuples cannot change, they need only two methods: count things and find things.
Think about it
Exams ask which methods tuples have and what operations are legal. Counting how many times a result occurs in a fixed data record.
`t.
count(x)` returns 0 if x is absent.
`t.
index(x, start, end)` raises ValueError if x is not found, so check with in first.
Since there is no append or remove, changing contents means creating a new tuple.
sorted(t) returns a LIST, not a tuple, so wrap it with tuple(sorted(t)) if you need a tuple.
sum, min and max work when the items are comparable.
`tuple(iterable)` converts a list, string, set or range into a tuple.
Tuple comparison is lexicographic: (1, 2) < (1, 3) is True.
Tuples can be compared and sorted, which is why they are used as sort keys.
Key points
count(): Number of occurrences of a value.index(): Position of the first occurrence; ValueError if missing.tuple(): Constructor converting an iterable to a tuple.
Read more
Example
CodeTuple methods and helper functions:
t = (3, 1, 2, 3, 3)
print(t.count(3)) # 3
print(t.index(2)) # 2
print(t.index(3, 1)) # 3 (search starts at position 1)
print(t.count(9)) # 0
if 9 in t:
print(t.index(9)) # safe: only runs when present
print(len(t), min(t), max(t), sum(t)) # 5 1 3 12
print(sorted(t)) # [1, 2, 3, 3, 3] (a list)
print(tuple(sorted(t))) # (1, 2, 3, 3, 3)
print(tuple("abc")) # ('a', 'b', 'c')
print(tuple(range(3))) # (0, 1, 2)
print((1, 2) < (1, 3)) # True
# t.append(4) -> AttributeError: tuple has no appendSolved examples
What is the output of t = (1, 2, 2, 3); print(t.count(2), t.index(3))?
Two numbers
Formula Applied:—Approach note: Index starts from 0.
Swap the values of a = 5 and b = 8 without a temporary variable.
Swapped values
Formula Applied:—Approach note: The right side is fully evaluated before assignment.
Change the first item of t = (1, 2, 3) to 100.
New tuple
Formula Applied:—Approach note: This builds a new tuple; the old one is not modified.
Unpack t = (5, 10, 15, 20) into a, *mid, b and print a, mid, b.
Unpacked values
Formula Applied:—Approach note: Exactly one starred target is allowed in an unpack.
Real-world applications
Using Python Tuples in real projects
Coordinates, RGB colours, database rows, returning multiple values from a function.
Common mistakes
❌ Myth: Tuples are just slow lists.
Why this is wrong: Tuples are slightly faster and smaller.
✅ Verified Truth: They are chosen for safety and hashability.
⚠️ Exam Tip: Mention dict keys in interviews.
❌ Myth: An immutable tuple can never change.
Why this is wrong: Only the tuple structure is fixed.
✅ Verified Truth: A list inside a tuple can still be modified.
⚠️ Exam Tip: Immutability is shallow.
❌ Myth: Parentheses make a tuple.
Why this is wrong: Commas make a tuple.
✅ Verified Truth: 1, 2 is a tuple; (1) is not.
⚠️ Exam Tip: Single-item tuples need a trailing comma.
Revision notes
A tuple is an ordered, immutable collection. You will learn how to create tuples, pack and unpack them, slice them, and use their two methods count() and index().
High-Yield Quick Revision Points
- Tuple: ordered, immutable, ( )
- Comma creates tuple: (5,)
- Unpacking: a, b = t; a, *rest = t
- Only count() and index()
- sorted(t) returns a list
- Hashable if all items hashable
Practice questions
Which type is (7)?
Which methods does a tuple have?
What does a, *b = (1, 2, 3) give for b?
What does sorted((3, 1, 2)) return?
What happens with t[0] = 5 on a tuple?
A function returning "return a, b" returns: