Tuples: Operations & Methods
What is a tuple?
A tuple is an ordered, immutable sequence of items written in round brackets ( ). It is like a list that is "locked" after creation — perfect for data that should not change, such as the days of the week, the date of birth or the GPS location of your school.
Creating tuples
t = (10, 20, 30, 40) print(t[1], t[-1], t[1:3], len(t), type(t)) one = (5,) # comma makes it a tuple not_tuple = (5) # just the number 5 in brackets print(type(one), type(not_tuple)) p = 1, 2, 3 # brackets are optional (packing) print(p, type(p))
Tuples are immutable
t = (1, 2) t[0] = 9
You also cannot append, insert or remove. You can delete the whole tuple with del t, or make a new tuple using +. If a tuple contains a mutable item like a list, that inner list can still change:
t = ([1, 2], 3) t[0].append(9) # the list inside can still change print(t)
Operations, methods and functions
t = (10, 20, 30, 40)
print((1, 2) + (3, 4), ('ab',) * 3, 20 in t)
x = (3, 7, 1, 7, 9)
print(x.count(7), x.index(9))
print(len(x), max(x), min(x), sum(x))
print(sorted(x)) # returns a LIST
print(tuple('hello'), tuple([1, 2]))| Feature | Example |
|---|---|
| Indexing / slicing | t[0], t[-1], t[1:3], t[::-1] |
| Concatenation / repetition | t1 + t2, t * 3 |
| Membership | x in t |
| Methods | t.count(x), t.index(x) |
| Functions | len, max, min, sum, sorted (gives list), tuple() |
Tuple assignment (packing and unpacking)
Packing puts many values into one tuple (p = 1, 2, 3). Unpacking takes them out into separate variables. The number of variables must equal the number of items, otherwise Python raises a ValueError ("too many values to unpack").
a, b, c = (4, 5, 6) # unpacking print(a, b, c) name, roll, marks = 'Ishan', 12, 88.5 print(name, roll, marks) x, y = 10, 20 x, y = y, x # swap = tuple packing + unpacking print(x, y)
Nested tuples
students = (('Riya', 91), ('Aman', 85), ('Zoya', 78))
print(students[1][0], students[0][1])
for name, m in students:
print(f'{name:<6}{m}')Tuple vs list
List [ ]
- Mutable – can add, remove, change
- Many methods (append, sort …)
- Use for data that changes: cart items, marks entry
Tuple ( )
- Immutable – fixed after creation
- Only count() and index()
- Faster, safer; can be a dictionary key
- Use for fixed data: months, (x, y) points
Solved programs
1. Input numbers into a tuple and find statistics
n = int(input('How many numbers? '))
t = ()
for i in range(n):
t = t + (int(input('Number: ')),) # build a new tuple each time
print('Tuple:', t)
print('Max:', max(t), '| Min:', min(t), '| Mean:', sum(t) / len(t))2. Function returning a tuple
def circle(r):
area = 3.14 * r * r
circumference = 2 * 3.14 * r
return area, circumference # a tuple
result = circle(5)
print(result, type(result))
a, c = circle(5)
print('Area =', a, '| Circumference =', c)The long decimal comes from float approximation; use round(c, 2) to show 31.4.
Summary & practice
- Tuple = ordered, immutable, in
( ); single item needs a comma. - Supports indexing, slicing, +, *, in, len, max, min, sum.
- Methods: count(), index(). sorted() returns a list.
- Packing/unpacking makes swapping and multiple returns easy.
Predict: t = (2, 4, 6, 8); print(t[1:3] * 2, t.index(6), 8 in t). Write a program to find the second largest element of a tuple and to count how many elements are even.
Answer: (4, 6, 4, 6) 2 True
