Tuples
Tuples
Sometimes you want to store many values together like a list.
But you may also want the values to stay safe and unchanged.
Python gives us tuples for this.
What Is a Tuple?
A tuple is a collection of values stored in one variable.
A tuple is similar to a list, but it cannot be changed after it is created.
This is called immutable.
Tuples use round brackets ().
colors = ("Red", "Blue", "Green")
print(colors)
Output
('Red', 'Blue', 'Green')
Why Tuples Matter
Tuples help you:
- Store fixed data
- Protect values from accidental changes
- Return many values from functions
- Keep related data together
Create a Tuple
numbers = (10, 20, 30)
cities = ("New York", "Toronto", "Chicago")
Read Items
Tuple items use indexes.
The first item starts at 0.
cities = ("New York", "Toronto", "Chicago")
print(cities[0])
print(cities[1])
Output
New York
Toronto
Negative Indexes
Negative indexes count from the end.
print(cities[-1])
print(cities[-2])
Output
Chicago
Toronto
Slicing Tuples
You can take part of a tuple using slicing.
cities = ("New York", "Toronto", "Chicago", "Vancouver")
print(cities[1:3])
print(cities[:2])
print(cities[2:])
Output
('Toronto', 'Chicago')
('New York', 'Toronto')
('Chicago', 'Vancouver')
Tuples Cannot Be Updated
You cannot change an item.
cities = ("New York", "Toronto")
cities[0] = "Boston"
This gives an error.
Tuples Cannot Add or Remove Items
These do not work on tuples:
append()remove()pop()
Because tuples are fixed after creation.
Recreate a Tuple
If you need changes, create a new tuple.
cities = ("New York", "Toronto")
cities = cities + ("Chicago",)
print(cities)
Output
('New York', 'Toronto', 'Chicago')
Length of a Tuple
Use len().
cities = ("New York", "Toronto", "Chicago")
print(len(cities))
Output
3
Count Values
Use count() to count repeats.
numbers = (1, 2, 2, 3)
print(numbers.count(2))
Output
2
Find Position
Use index().
cities = ("New York", "Toronto", "Chicago")
print(cities.index("Toronto"))
Output
1
Loop Through a Tuple
cities = ("New York", "Toronto", "Chicago")
for city in cities:
print(city)
Output
New York
Toronto
Chicago
Unpacking a Tuple
You can place items into variables.
name, age = ("Tom", 25)
print(name)
print(age)
Output
Tom
25
Returning Many Values
Functions often return tuples.
def user():
return "Sara", 30
data = user()
print(data)
Output
('Sara', 30)
Code Along
sizes = ("Small", "Medium", "Large")
for size in sizes:
print(size)
Output
Small
Medium
Large
Mini Challenge
Build a country tuple.
Steps:
- Create a tuple with:
USA,Canada,Mexico - Print the first item
- Print the last item
- Print the first two items using slicing
- Print the total number of items
Expected output:
USA
Mexico
('USA', 'Canada')
3
Real World Use Case
Programs use tuples for coordinates, settings, fixed categories, and values that should not change.
Quiz
- What is a tuple?
- Can tuples be changed after creation?
- Which brackets are used for tuples?
- Can tuples be sliced?
Assignment
Create a tuple of 4 months. Print each month using a loop, then print the last two months using slicing.
Summary
You learned that tuples store many values like lists, but tuples are fixed after creation and are best for data that should not change.