Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Fixed Ordered Collections

Tuples keep ordered data fixed once it is defined

Lists are great when you expect change. Tuples are better when the values represent one fixed record or coordinate that should keep the same shape over time. They still preserve order, but they trade mutability for stability and clarity.

Core Explanation

Use tuples when:

  • order matters

  • values should not be changed accidentally

  • the data represents a fixed record such as coordinates, dates, or configuration settings

Continuity from lists

Lists and tuples are both ordered sequences. The key distinction is this: lists are designed for change, while tuples are designed for fixed structure.

location = (19.07, 72.88)
print(location[0])
print(location[1])

Worked Example: one record, several fixed values

The code above treats a location as one stable record: latitude first, longitude second. That is a common tuple pattern: a small group of related values whose meaning depends on position and should not be changed casually.

Exercises

  1. Create a tuple representing a month and its revenue.

  2. Explain one reason to use a tuple instead of a list.

  3. Access the first and last values in a tuple.

  4. Describe one business example where changing the stored values accidentally would be a problem.

Hint

A tuple is often a good fit when the number and meaning of the fields should stay fixed, such as (month, revenue) or (latitude, longitude).

Quick Summary
  • tuples are ordered collections

  • tuples are immutable

  • tuples are useful for fixed groups of related values

  • tuples often represent records better than lists when accidental changes should be avoided

Practice Lab

Expected output
(19.07, 72.88)
19.07
Expected output
4
1

Tuple Methods

Tuples expose only two built-in methods, so it helps to practice them separately.

count()

Use count() to measure how many times a value appears in a tuple.

index()

Use index() to find the first position of a value in the tuple.

Guided Practice

What is a key property of tuples?

They always contain only numbersTuples can hold many types of values.
They are immutableCorrect. Tuples cannot be modified after creation.
They remove duplicates automaticallyThat is a set property, not a tuple property.
They store key-value pairsDictionaries store key-value pairs.

What does `quarter.count(120)` return above?

0The value 120 is present.
1Correct. The value appears once.
4That is the length of the tuple.
120`count()` returns frequency, not the value itself.

Key Takeaway

Tuples are best when order matters but the structure should stay fixed. They help communicate that a group of values belongs together and should not be changed casually.

Deeper Python Tuple Patterns

Adapted for this course from the Python Tutorial data structures chapter.

This section extends the basic tuple idea into patterns that matter in real Python code:

  • tuple packing and unpacking

  • singleton and empty tuples

  • nested tuples

  • sequence behavior across fixed ordered values

How to read this section

Focus on what tuples make easier to express: fixed shape, unpacking into variables, and returning or swapping multiple values cleanly.

# Tuple basics
t = 12345, 54321, 'hello!'
print(t[0])
print(t)

# Nested tuples
u = t, (1, 2, 3, 4, 5)
print(u)

# Tuples are immutable (uncomment to see the error)
# t[0] = 88888

# Singleton tuple and empty tuple
empty = ()
singleton = 'hello',
print(len(empty), singleton)

# Sequence unpacking
x, y, z = t
print('Unpacked:', x, y, z)

# Tuple assignment (packing + unpacking)
a = 1, 2
b = 3, 4
a, b = b, a
print('Swapped:', a, b)