Unit 4 • Lesson 5

Tuples and Immutable Data

Overview

Tuples are like lists, but their data can't change once created. You'll see why immutability can make code safer and faster and when tuples are the better choice, especially for fixed data that shouldn't be modified accidentally.

Beginner 15–20 min

What You Will Learn in This Lesson

By the end of this lesson, you will know:

  • What tuples are: Understand how tuples differ from lists.
  • Immutability: Learn what it means for data to be immutable.
  • Creating tuples: Discover how to create and use tuples.
  • When to use tuples: Understand when tuples are better than lists.

Why This Matters

Tuples are similar to lists, but they can't be changed after creation. This immutability makes them safer for data that shouldn't be modified, like coordinates, dates, or configuration settings. Tuples are also slightly faster than lists and can be used as dictionary keys, making them useful in many situations!

Step 1: What Is a Tuple?

A tuple is an ordered collection of items, just like a list, but unlike lists, tuples are immutable - once created, they cannot be changed.

Creating Tuples
# Tuple with parentheses
coordinates = (10, 20)

# Tuple without parentheses (comma makes it a tuple)
point = 5, 10

# Single-item tuple (note the comma)
single = (42,)

print(coordinates)  # Output: (10, 20)
1

Parentheses

Tuples are created using parentheses () instead of square brackets. However, the parentheses are optional - a comma is what actually makes it a tuple!

2

Comma Required

For a single-item tuple, you must include a comma after the item: (42,). Without the comma, Python treats it as just a number in parentheses, not a tuple.

3

Ordered

Like lists, tuples maintain the order of items. The first item stays first, the second stays second, and so on.

Key Concept: Tuples are immutable, which means once you create a tuple, you cannot add, remove, or change its items. This makes tuples perfect for data that should stay fixed, like coordinates, dates, or any information that shouldn't be accidentally modified!

Mini Practice #1: Creating Tuples

Try It Yourself

Try creating different tuples:

Press Run to see output

What happened? You created tuples using parentheses. Notice how tuples look similar to lists, but use parentheses instead of square brackets. Tuples are perfect for fixed data like coordinates or color values that shouldn't change!

Step 2: Tuples vs Lists

The main difference between tuples and lists is that tuples are immutable (cannot be changed) while lists are mutable (can be changed):

Lists vs Tuples
# List - can be changed
my_list = [1, 2, 3]
my_list[0] = 10  # This works!
print(my_list)  # Output: [10, 2, 3]

# Tuple - cannot be changed
my_tuple = (1, 2, 3)
# my_tuple[0] = 10  # This would cause an error!

Lists

Mutable - can add, remove, or change items.

[1, 2, 3]

Use square brackets

Tuples

Immutable - cannot change after creation.

(1, 2, 3)

Use parentheses

Why Immutability Matters

Immutability protects your data from accidental changes. If you have coordinates like (5, 10), you know they'll always be (5, 10) - no code can accidentally modify them. This makes tuples safer for important data that should stay fixed!

Step 3: Accessing Tuple Elements

You access tuple elements the same way you access list elements - using indices:

Accessing Tuple Elements
point = (10, 20, 30)

print(point[0])  # Output: 10 (first element)
print(point[1])  # Output: 20 (second element)
print(point[-1])  # Output: 30 (last element)

Remember: You can read tuple elements using indexing, just like lists. You can use positive indices (0, 1, 2...) or negative indices (-1, -2, -3...). However, you cannot modify tuple elements - they're read-only!

Mini Practice #2: Working with Tuples

Try It Yourself

Try accessing and using tuple elements:

Press Run to see output

What happened? You accessed tuple elements using indices, just like with lists. Tuples work similarly to lists for reading data - you can access elements, get the length, and loop through them. The key difference is that you cannot modify tuples after they're created!

Step 4: When to Use Tuples

Use tuples when:

1

Data Shouldn't Change

When you have data that should stay fixed, like coordinates, dates, or configuration values. Tuples prevent accidental modifications.

2

Dictionary Keys

Tuples can be used as dictionary keys (lists cannot). This is useful when you need to use multiple values as a key.

3

Returning Multiple Values

Functions can return tuples to give back multiple values at once. This is a common pattern in Python.

Best Practice

Use lists when you need to modify the data (add, remove, or change items). Use tuples when the data should stay fixed. If you're not sure, start with a list - you can always convert it to a tuple later if needed!

End-of-Lesson Exercises

Exercise 1: Create and Access a Tuple

Create a tuple called coordinates with values (10, 20). Print the first element (x-coordinate) and the second element (y-coordinate).

Use parentheses to create a tuple and indices to access elements.

Write your code above and click "Check Answer" to verify it's correct.

Exercise 2: Tuple Operations

Create a tuple called info with three values: ("Python", 3, "Beginner"). Print the length of the tuple, access the first element, and try to print all elements using a loop.

Use len() for length, indexing for access, and a for loop to iterate.

Write your code above and click "Check Answer" to verify it's correct.