1. Introduction to Tuples
A tuple is an ordered collection of elements enclosed in parentheses (). Tuples can contain elements of different data types.
Tuples are immutable — once created, their elements cannot be changed, added, or removed.
2. Creating Tuples
# Empty tuple
empty = ()
# Single-element tuple (note the trailing comma)
single = (5,)
# Tuple of integers
numbers = (1, 2, 3, 4, 5)
# Mixed data types
mixed = (10, "Hello", 3.14, True)
# Without parentheses (tuple packing)
packed = 1, 2, 3
# Using tuple() constructor
chars = tuple("Python") # ('P', 'y', 't', 'h', 'o', 'n')3. Tuple Indexing
tup = (10, 20, 30, 40, 50)
# Index: 0 1 2 3 4
# Neg: -5 -4 -3 -2 -1
print(tup[0]) # 10
print(tup[2]) # 30
print(tup[-1]) # 50 (last element)
print(tup[-3]) # 304. Tuple Slicing
tup = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
print(tup[2:6]) # (2, 3, 4, 5)
print(tup[:4]) # (0, 1, 2, 3)
print(tup[5:]) # (5, 6, 7, 8, 9)
print(tup[::2]) # (0, 2, 4, 6, 8)
print(tup[::-1]) # (9, 8, 7, 6, 5, 4, 3, 2, 1, 0)5. Tuples are Immutable
tup = (1, 2, 3)
# tup[0] = 10 # TypeError: 'tuple' object does not support item assignment
# tup.append(4) # AttributeError: 'tuple' object has no attribute 'append'
# But if a tuple contains a mutable object like a list, that object can be modified
tup = (1, [2, 3], 4)
tup[1].append(5)
print(tup) # (1, [2, 3, 5], 4)6. Tuple Operators
# Concatenation (+)
a = (1, 2, 3)
b = (4, 5, 6)
print(a + b) # (1, 2, 3, 4, 5, 6)
# Repetition (*)
print((0,) * 5) # (0, 0, 0, 0, 0)
# Membership (in)
print(3 in a) # True
print(7 not in a) # True7. Tuple Methods
Tuples have only two built-in methods:
| Method | Description | Example | Result |
|---|---|---|---|
| index(x) | Returns index of first occurrence | (1,2,3,2).index(2) | 1 |
| count(x) | Counts occurrences of x | (1,2,2,3).count(2) | 2 |
Other useful built-in functions: len(), max(), min(), sum(), sorted(), any(), all().
8. Tuple Packing and Unpacking
# Packing
coordinates = 10, 20, 30
print(coordinates) # (10, 20, 30)
# Unpacking
x, y, z = coordinates
print(x, y, z) # 10 20 30
# Swapping using tuple unpacking
a, b = 5, 10
a, b = b, a
print(a, b) # 10 59. Nested Tuples
nested = ((1, 2), (3, 4), (5, 6))
print(nested[0]) # (1, 2)
print(nested[1][1]) # 4
# Traversing nested tuple
for inner in nested:
for element in inner:
print(element, end=" ")
print()10. List vs Tuple Comparison
| Feature | List | Tuple |
|---|---|---|
| Syntax | [] | () |
| Mutability | Mutable | Immutable |
| Speed | Slower | Faster |
| Methods | Many (append, insert, etc.) | Only 2 (index, count) |
| Use case | When data may change | When data is constant (like coordinates, days of week) |
| Hashable? | No | Yes (can be used as dictionary key) |
11. Revision Questions and Answers
Very Short Answer Questions
1. What is a tuple in Python?
An ordered, immutable collection of elements enclosed in parentheses.
2. Are tuples mutable or immutable?
Immutable.
3. How do you create a single-element tuple?
(5,) — trailing comma is mandatory.
4. Which two methods are available for tuples?
index() and count().
5. Can a tuple be used as a dictionary key?
Yes, because tuples are hashable (immutable).
Short Answer Questions
1. Differentiate between lists and tuples.
Lists are mutable (can be modified), tuples are immutable. Lists use [], tuples use (). Lists have more methods (append, extend, etc.), tuples have only index() and count(). Tuples are faster and can be used as dictionary keys.
2. Explain tuple packing and unpacking with examples.
Packing: t = 1, 2, 3 creates a tuple (1, 2, 3) without parentheses. Unpacking: a, b, c = t assigns values to individual variables. This is useful for swapping: a, b = b, a.
Long Answer Questions
1. Write a program that stores student information as a tuple (roll_no, name, marks) and displays the data.
students = [(101,"Riya",85), (102,"Amit",92), (103,"Priya",78)]; for roll, name, marks in students: print(f"Roll: {roll}, Name: {name}, Marks: {marks}")
2. Write a Python program to find the maximum, minimum, and sum of elements in a tuple.
tup = (45, 12, 78, 34, 90, 23); print("Max:", max(tup)); print("Min:", min(tup)); print("Sum:", sum(tup))