Python Basics · Lesson 8
Tuples
Learn how Python tuples store ordered collections of values, access items by index, unpack values and understand why tuples cannot be changed after creation.
What you will learn
- • Creating tuples
- • Tuple indexing
- • Negative indexing
- • Tuple immutability
- • Tuple unpacking
- • len()
- • count()
- • index()
- • Tuple vs List
- • Real-world tuple examples
Part 1
What is a tuple?
A tuple is an ordered collection of values. Tuples are similar to lists, but tuples are immutable, which means their existing values cannot be changed after the tuple is created.
colors = ("red", "green", "blue")
print(colors)CHECK YOUR UNDERSTANDING
Which brackets are commonly used to create a tuple?
Complete the tuple: colors = _____
Creating tuples
A tuple can contain multiple values separated by commas.
student = ("Aman", 21, "Python")
print(student)CHECK YOUR UNDERSTANDING
How many values are stored in the student tuple?
CHECK YOUR UNDERSTANDING
Which value is at index 0?
The single-item tuple
A single-item tuple needs a trailing comma. The comma is what makes it a tuple.
Tuple
item = ("Python",)Not a tuple
item = ("Python")CHECK YOUR UNDERSTANDING
Which creates a single-item tuple?
Tuple indexing
Tuples use zero-based indexing just like strings and lists.
numbers = (10, 20, 30, 40)
print(numbers[0])
print(numbers[2])CHECK YOUR UNDERSTANDING
What does numbers[0] return?
CHECK YOUR UNDERSTANDING
What does numbers[2] return?
Negative indexing
Negative indexes access values from the end of a tuple. Index -1 refers to the last item.
months = ("Jan", "Feb", "Mar", "Apr")
print(months[-1])CHECK YOUR UNDERSTANDING
What does months[-1] return?
Access the last tuple item: months[_____]
Important concept
Tuples are immutable
Once a tuple has been created, its existing elements cannot be changed directly.
numbers = (10, 20, 30)
numbers[1] = 50CHECK YOUR UNDERSTANDING
What happens when you try to change numbers[1]?
Key idea:
If you need a collection whose existing items can be changed, a list is generally more appropriate.
len() with tuples
The len() function returns the number of items in a tuple.
cities = ("Delhi", "Dehradun", "Mumbai")
print(len(cities))CHECK YOUR UNDERSTANDING
What will len(cities) return?
Complete: total = _____(cities)
count()
The count() method tells you how many times a value appears in a tuple.
numbers = (10, 20, 10, 30, 10)
print(numbers.count(10))CHECK YOUR UNDERSTANDING
What does numbers.count(10) return?
Complete: numbers._____(20)
index()
The index() method returns the position of the first occurrence of a value.
colors = ("red", "green", "blue")
print(colors.index("green"))CHECK YOUR UNDERSTANDING
What does colors.index("green") return?
Complete: colors._____("blue")
Important skill
Tuple unpacking
Tuple unpacking allows the values inside a tuple to be assigned to separate variables.
student = ("Aman", 21, "Python")
name, age, course = student
print(name)
print(age)
print(course)CHECK YOUR UNDERSTANDING
What value will be stored in age?
CHECK YOUR UNDERSTANDING
How many variables are needed to unpack this tuple: data = ('A', 'B', 'C')?
Complete: name, age, course _____ student
Predict the output
Think before running the code
data = ("Python", "SQL", "Excel")
a, b, c = data
print(b)CHECK YOUR UNDERSTANDING
What will the program print?
Compare
Tuple vs List
| Feature | List | Tuple |
|---|---|---|
| Syntax | [ ] | ( ) |
| Mutable | Yes | No |
| Ordered | Yes | Yes |
CHECK YOUR UNDERSTANDING
Which statement correctly describes a tuple?
CHECK YOUR UNDERSTANDING
Which structure should you generally use when you need to modify existing items?
Debug the tuple code
student = ("Aman", 21, "Python")
student[1] = 22
print(student)CHECK YOUR UNDERSTANDING
Why does this code fail?
Real-world example
Store fixed information
Tuples can be useful for values that belong together and should remain unchanged, such as a coordinate, RGB color or fixed configuration.
location = (30.3165, 78.0322)
latitude, longitude = location
print(latitude)
print(longitude)CHECK YOUR UNDERSTANDING
What does tuple unpacking allow us to do here?
Practice
Complete the tuple operations
Find the number of items: total = _____(data)
Count a value: data._____("Python")
Find a value position: data._____("SQL")
Access the first tuple item: data[_____]
Access the last tuple item: data[_____]
Mini Challenge
Unpack a student record
Create a tuple containing a student's name, age and course. Unpack the tuple into three variables and display each value.
student = ("Priya", 22, "Data Analytics")
name, age, course = student
print(name)
print(age)
print(course)Try changing the values and run the program.
Final Challenge
Build a fixed sales record
Create a tuple containing a product name, price and quantity. Unpack the values into separate variables and calculate the revenue.
sale = ("Laptop", 55000, 2)
product, price, quantity = sale
revenue = price * quantity
print(product)
print(revenue)Expected revenue: ₹110000
Lesson recap
- ✓ Tuples are ordered collections.
- ✓ Tuples commonly use parentheses.
- ✓ Tuple indexes start at 0.
- ✓ -1 refers to the last item.
- ✓ Tuples are immutable.
- ✓ len() returns the number of items.
- ✓ count() counts occurrences.
- ✓ index() finds the position of a value.
- ✓ Tuple unpacking assigns values to variables.
- ✓ Lists are mutable while tuples are immutable.