CodingNic

Data Structures

Tuples

Data Structures 22 min read

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 ().

python
colors = ("Red", "Blue", "Green")
print(colors)

Output

text
('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

python
numbers = (10, 20, 30)
cities = ("New York", "Toronto", "Chicago")

Read Items

Tuple items use indexes.

The first item starts at 0.

python
cities = ("New York", "Toronto", "Chicago")

print(cities[0])
print(cities[1])

Output

text
New York
Toronto

Negative Indexes

Negative indexes count from the end.

python
print(cities[-1])
print(cities[-2])

Output

text
Chicago
Toronto

Slicing Tuples

You can take part of a tuple using slicing.

python
cities = ("New York", "Toronto", "Chicago", "Vancouver")

print(cities[1:3])
print(cities[:2])
print(cities[2:])

Output

text
('Toronto', 'Chicago')
('New York', 'Toronto')
('Chicago', 'Vancouver')

Tuples Cannot Be Updated

You cannot change an item.

python
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.

python
cities = ("New York", "Toronto")
cities = cities + ("Chicago",)

print(cities)

Output

text
('New York', 'Toronto', 'Chicago')

Length of a Tuple

Use len().

python
cities = ("New York", "Toronto", "Chicago")
print(len(cities))

Output

text
3

Count Values

Use count() to count repeats.

python
numbers = (1, 2, 2, 3)
print(numbers.count(2))

Output

text
2

Find Position

Use index().

python
cities = ("New York", "Toronto", "Chicago")
print(cities.index("Toronto"))

Output

text
1

Loop Through a Tuple

python
cities = ("New York", "Toronto", "Chicago")

for city in cities:
    print(city)

Output

text
New York
Toronto
Chicago

Unpacking a Tuple

You can place items into variables.

python
name, age = ("Tom", 25)

print(name)
print(age)

Output

text
Tom
25

Returning Many Values

Functions often return tuples.

python
def user():
    return "Sara", 30

data = user()
print(data)

Output

text
('Sara', 30)

Code Along

python
sizes = ("Small", "Medium", "Large")

for size in sizes:
    print(size)

Output

text
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:

text
USA
Mexico
('USA', 'Canada')
3

Real World Use Case

Programs use tuples for coordinates, settings, fixed categories, and values that should not change.

Quiz

  1. What is a tuple?
  2. Can tuples be changed after creation?
  3. Which brackets are used for tuples?
  4. 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.