Skip to main content

Access Tuple Items

To read a tuple item, put its index number inside square brackets:

Example

Index 1 gives us the second item:

Prompt
Create a tuple with three fruits and print the second item using its index.
Output: complete Python program

Negative Indexing

A negative index counts backward from the end of the tuple.

So -1 is the final item, -2 is the one just before it, and so on.

Example

Get the final item and print it:

Prompt
Create a tuple with three fruits and print the last item using negative indexing.
Output: complete Python program

Range of Indexes

To get several items at once, give a start and an end for the range.

The result comes back as a new tuple holding just those items.

Example

Get the items in third, fourth and fifth position:

Prompt
Create a tuple with seven fruits and print items from index 2 to 5 (not included) using a range of indexes.
Output: complete Python program

Example

Leave out the start and the range begins at the first item, stopping just before "kiwi":

Prompt
Create a tuple with seven fruits and print items from the beginning to index 4 (not included).
Output: complete Python program

Example

Leave out the end and the range runs from "cherry" through the last item:

Prompt
Create a tuple with seven fruits and print items from index 2 to the end.
Output: complete Python program

Range of Negative Indexes

A range can also use negative indexes, which count back from the end of the tuple:

Example

The range starts at index -4, which is included, and stops at index -1, which is left out:

Prompt
Create a tuple with seven fruits and print items from index -4 (included) to index -1 (excluded).
Output: complete Python program

Check if Item Exists

The in keyword tells you whether a given item is in a tuple:

Example

Test whether the tuple contains "apple":

Prompt
Create a tuple with three fruits and check if "apple" is present using the in keyword.
Output: complete Python program