Python Tuples

Let’s look at tuples

  1. Notice

    A tuple is an immutable sequence. Unpacking binds each name to the corresponding element; a one-element tuple needs a comma.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
point = (3, 4)
x, y = point
print(x)
print(y)
Live state

Top-level scope

point→Object 1
x→Object 2
y→Object 3
Object 1 · tuple(3, 4)Object 2 · int3Object 3 · int4
This step

A tuple is an immutable sequence. Unpacking binds each name to the corresponding element; a one-element tuple needs a comma.

Picture tuples

A tuple is an immutable sequence. Unpacking binds each name to the corresponding element; a one-element tuple needs a comma.

Image brief

Alt text
A diagram for Python Tuples. Instructions: point = (3, 4) x, y = point print(x) print(y). Displayed result: 3 4. The final example bindings are point --> object 1 (tuple); x --> object 2 (int); y --> object 3 (int). object 1: (3, 4); object 2: 3; object 3: 4
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python Tuples". Teaching focus: A tuple is an immutable sequence. Unpacking binds each name to the corresponding element; a one-element tuple needs a comma. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS point = (3, 4) x, y = point print(x) print(y) | v DISPLAYED RESULT 3 4 FINAL BINDINGS point --> object 1 (tuple) x --> object 2 (int) y --> object 3 (int) object 1: (3, 4) object 2: 3 object 3: 4 Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python Tuples. Instructions: point = (3, 4) x, y = point print(x) print(y). Displayed result: 3 4. The final example bindings are point --> object 1 (tuple); x --> object 2 (int); y --> object 3 (int). object 1: (3, 4); object 2: 3; object 3: 4

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
point = (3, 4)
x, y = point
print(x)
print(y)
Live state

Top-level scope

point→Object 1
x→Object 2
y→Object 3
Object 1 · tuple(3, 4)Object 2 · int3Object 3 · int4
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
3
4

What do you expect this program to print?

What does this example display?

point = (3, 4)
x, y = point
print(x)
print(y)
What does this example display?
Choose one answer. You can try again.

Answer and explanation

3 4

Follow the printed values in order. The output is: 3 4

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

A tuple is an immutable sequence. Unpacking binds each name to the corresponding element; a one-element tuple needs a comma.

A tuple is an immutable sequence. Unpacking binds each name to the corresponding element; a one-element tuple needs a comma.

What Is a Tuple?

A tuple is a collection of items, just like a list, but it cannot be changed after it's created. Tuples are written using round brackets ().

Example:

fruits = ("apple", "banana", "cherry")

Here, we created a tuple called fruits that holds three items. The round brackets mean it's a tuple.

Why Use Tuples?

  • You want to keep your data safe and unchangeable.
  • You want to group related values together, like a pair of coordinates: (x, y).
  • Tuples are faster than lists and take less memory.

1. Create Tuple

This example shows how to create a tuple in Python using parentheses (). Here, fruits is a tuple containing three string elements.

Note: Even a single-element tuple must have a trailing comma, like single = ("apple",) to be recognized as a tuple.

fruits = ("apple", "banana", "cherry")
('apple', 'banana', 'cherry')

2. Read Items in Tuple

Tuples in Python store items in an ordered way, meaning each item has a specific index starting from 0. To access an item, use square brackets with the index.

For example, fruits[0] retrieves the first item in the tuple, which is "apple".

Indexing allows direct access to any element: fruits[1] gives "banana", and fruits[2] gives "cherry".

fruits = ("apple", "banana", "cherry")
print(fruits[0])
print(fruits[1])
apple
banana

3. Check Length of Tuple

Use the built-in len() function to find the number of elements in the tuple fruits.

The syntax len(tuple_name) returns an integer indicating how many items are stored inside the tuple.

In this case, the tuple has 3 items, so len(fruits) returns 3.

fruits = ("apple", "banana", "cherry")
print(len(fruits))
3

This tells us the tuple has 3 items.

4. Loop Through a Tuple

Tuples, like lists, are iterable in Python. We can use a for loop to iterate over the items in a tuple.

fruits = ("apple", "banana", "cherry")
for fruit in fruits:
    print(fruit)
apple
banana
cherry
apple
banana
cherry

5. Check If Item Exists

The expression "banana" in fruits checks whether the string "banana" is present in the tuple fruits. The in keyword returns a boolean: True if the item exists, otherwise False.

fruits = ("apple", "banana", "cherry")
print("banana" in fruits)
True

Things You Cannot Do With Tuples

Because tuples are unchangeable:

  • You can't add items
  • You can't remove items
  • You can't change any item

Trying to Change a Tuple (will cause an error)

fruits[0] = "orange"
TypeError: 'tuple' object does not support item assignment

Python will give you an error if you try to change the content.

What to remember

Connect what you noticed in the example with the idea below.

Remember

A tuple is an immutable sequence. Unpacking binds each name to the corresponding element; a one-element tuple needs a comma.

Watch out for this idea

You can replace a tuple element by assigning to its index.

This is the misconception to avoid.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Python Sets →