Flat_Eric x 3, in boxes

Tuples

Introduction to Tuples

We’ve covered Lists (which are flexible and changeable) and Dictionaries (which map keys to values). Now, we meet the third major data structure: the Tuple.

What are Tuples?

A Tuple is a collection of items that is ordered, just like a list. However, there is one massive difference: Tuples are immutable.

Tuple Syntax

While lists use square brackets [], tuples use parentheses ().

Membership and Methods

Even though you can't change a tuple, you can still interact with it. Just like with lists and dictionaries, you can use the in operator to check for membership:

You can also use methods like .count() to see how many times an item appears, or .index() to find where an item is located.

The Trade-off: Safety vs. Flexibility

Using tuples is a strategic choice in programming:

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: Creating a Tuple

Exercise 2: The Immutability Crash

Exercise 3: Membership Check

Exercise 4: Indexing Tuples

Exercise 5: Finding the Position

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course