Flat_Eric x 3, in boxes holding fruits

More Tuples

More Tuples

Since you’ve already mastered Lists, you’ll find that navigating Tuples feels like second nature. Because both structures are "ordered," Python treats them like a sequence of numbered slots. This means every trick you learned for grabbing specific items from a list works here too. The only real difference is the "Stone Tablet" rule: while you can look at, measure, or slice a tuple as much as you want, the original sequence is locked in place. Using a slice doesn't change the original; it just creates a smaller, separate snapshot for you to use.

Slicing in Action

Just like with lists, you use the colon : within square brackets to specify a range. It follows the standard pattern: [start:stop:step], where the start index is included, but the stop index is not.

Key Behaviors to Note:

Unpacking and Methods

Similarly to lists, we can use variables to "unpack" and print parts of the tuple. For example:

We could print (a), or (a, b, c), or (a, b, c, *other), or even (other). The *other syntax tells Python to grab everything left over after the first variables are filled and put it into a list.

For tuples, we only have two methods to worry about:

We can also determine the length of a tuple using the built-in len() function, just like we do with lists and strings.

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1: Basic Slicing

Exercise 2: Unpacking the Basics

Exercise 3: The Asterisk (*) Catch-all

Exercise 4: Counting Occurrences

Exercise 5: Finding the Length

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