Module 2 Lesson 3: Tuples
·Programming

Module 2 Lesson 3: Tuples

Discover the power of immutability. Learn what Tuples are, how to use them, and why they are faster than Lists.

Module 2 Lesson 3: Tuples

In the previous lesson, we learned that Lists are Mutable (meaning they can be changed). In this lesson, we meet the Tuple. A Tuple is an ordered collection that is Immutable—once you create it, you cannot change, add, or remove its items.

Lesson Overview

In this lesson, we will cover:

  • Creating Tuples: Using parentheses ().
  • Immutability: Why "locked" data is useful.
  • Tuple Packing & Unpacking: A Python secret weapon.
  • When to use Tuples: Tuples vs. Lists.

1. Creating a Tuple

Tuples use parentheses () instead of square brackets.

dimensions = (1920, 1080)
coordinates = (40.7128, -74.0060) # Latitude, Longitude of NYC
colors = ("Red", "Green", "Blue")

Note: You can access items in a tuple using indexing, just like lists!

print(dimensions[0]) # Output: 1920

2. The Power of Immutability

What happens if you try to change a tuple?

days = ("Monday", "Tuesday", "Wednesday")
# days[0] = "Holiday" # This will cause a TypeError!

Why is this good?

  1. Safety: You can ensure that important data (like server settings or constants) isn't accidentally overwritten.
  2. Performance: Python can process Tuples faster than Lists because it knows the size will never change.
  3. Dictionary Keys: Only immutable items can be used as keys in a Dictionary (more on that next lesson!).

3. Tuple Packing and Unpacking

This is where Tuples get really cool. You can "pack" multiple values into one variable and "unpack" them back into multiple variables.

# Packing
user_data = ("Alex", 25, "Designer")

# Unpacking
name, age, job = user_data

print(name) # Alex
print(age)  # 25

Pro Tip: This is how Python functions can "return" multiple values at once!


4. Tuples vs. Lists: A Summary

FeatureList []Tuple ()
Changeable?Yes (Mutable)No (Immutable)
SpeedSlowerFaster
Use CaseGroups of items that changePermanent records/constants

Practice Exercise: The GPS Tracker

Create a file named gps.py.

  1. Create a tuple named location representing a latitude and longitude (e.g., (45.5230, -122.6765)).
  2. Try to change the latitude to a new value and observe the error.
  3. Create a tuple with your First Name, Last Name, and Hobby.
  4. Unpack that tuple into three separate variables: first, last, and hobby.
  5. Print an f-string using those variables: "My name is [first] [last] and I love [hobby]!"

Quick Knowledge Check

  1. What symbols do we use to create a Tuple?
  2. What does "immutable" mean?
  3. Which is faster to process: a List or a Tuple?
  4. Can you use .append() on a Tuple?

Key Takeaways

  • Tuples are ordered and unchangeable.
  • They are great for data that should stay constant throughout a program.
  • Unpacking allows you to extract all tuple items into separate variables in one line.
  • Use Tuples for performance and data safety.

What’s Next?

Lists and Tuples store values by their position (0, 1, 2...). But what if you wanted to store values by Labels? In Lesson 4, we’ll master Dictionaries—the most powerful way to store structured data in Python!

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn