Python Programming from First Principles: Learn to Code, Think Computationally, and Solve Real-World Problems
PDFEPUB
BOOK 000 · CHAPTER 3 · Tuples

Tuples

On this pageCreating a TupleAccessing Tuple ElementsUpdating TuplesUnpacking TuplesJoining TuplesTuple MethodsThe count() MethodThe index() MethodWhy Use Tuples?

Lists suit items that may change. For a fixed group of values, use a tuple. A tuple is an ordered sequence whose element references cannot be replaced, added or removed after creation. This makes the tuple immutable. If an element is itself a mutable object, such as a list, that object can still change.

Tuples are ideal for storing fixed collections of items, such as coordinates, months of the year, or settings that should not be altered. Tuples are commonly written with parentheses ( ); for a nonempty tuple, the commas create the tuple.

Creating a Tuple

To define a tuple, place several values inside parentheses, separated by commas.

fruits = ("apple", "banana", "cherry")
print(fruits)
Listing 161 — Creating a Tuple

Output

('apple', 'banana', 'cherry')

Even though they look similar to lists, tuples are much stricter — once created, their elements cannot be changed, added, or removed.

If a tuple has only one element, you must include a comma after it; otherwise, Python will not recognize it as a tuple:

single = ("apple", )
print(type(single))
Listing 162 — Creating a Tuple

Output

<class 'tuple'>

Accessing Tuple Elements

You can access tuple elements in the same way as lists, using indexing.

fruits = ("apple", "banana", "cherry")
print(fruits[0])   # first element
print(fruits[-1])  # last element
Listing 163 — Accessing Tuple Elements

Output

apple
cherry

Slicing also works with tuples, letting you extract parts of a sequence.

Updating Tuples

Tuples are immutable, meaning they cannot be directly modified. However, there are cases where you can simulate updates by converting a tuple into a list, changing it, and then converting it back.

fruits = ("apple", "banana", "cherry")

# Convert to list
temp_list = list(fruits)
temp_list[1] = "mango"

# Convert back to tuple
fruits = tuple(temp_list)
print(fruits)
Listing 164 — Updating Tuples

Output

('apple', 'mango', 'cherry')

This approach is common when you want the safety of tuples but still need occasional updates.

Unpacking Tuples

Unpacking means assigning each element of a tuple to separate variables in one step. This gives each value a useful name without separate indexing operations.

student = ("Alice", 21, "History")
name, age, course = student
print(name)
print(age)
print(course)
Listing 165 — Unpacking Tuples

Output

Alice
21
History

Unpacking makes code cleaner and easier to read, especially when working with small groups of related values.

Joining Tuples

Just like lists, tuples can be combined using the + operator. This creates a new tuple containing elements from both.

a = (1, 2, 3)
b = (4, 5)
c = a + b
print(c)
Listing 166 — Joining Tuples

Output

(1, 2, 3, 4, 5)

Because tuples cannot be changed, joining them always creates a new tuple rather than altering an existing one.

Tuple Methods

Although tuples are immutable, Python still provides a few useful built-in methods for working with them.

The count() Method

count() tells you how many times a specific value appears in the tuple.

numbers = (1, 2, 2, 3, 2, 4)
print(numbers.count(2))
Listing 167 — The count() Method

Output

3

The index() Method

index() returns the position of the first occurrence of a value.

numbers = (10, 20, 30, 40)
print(numbers.index(30))
Listing 168 — The index() Method

Output

2

These methods are especially helpful when analyzing or validating tuple data.

Why Use Tuples?

Tuples offer several advantages:

  • They protect data from accidental changes.
  • Tuples can use less memory than comparable lists. Choose them to express fixed structure rather than assuming every operation will be faster.
  • Tuples containing suitable immutable values can be dictionary keys, introduced later. A tuple containing a list cannot be a key.

In practice, tuples are perfect for fixed collections of items that define a single entity, like (latitude,longitude) for a location or (day,month,year) for a date.