Remember
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.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
point = (3, 4)
x, y = point
print(x)
print(y)Top-level scope
(3, 4)Object 2 · int3Object 3 · int4A tuple is an immutable sequence. Unpacking binds each name to the corresponding element; a one-element tuple needs a comma.
(no output)
What do you expect this program to print?
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: 4Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
point = (3, 4)
x, y = point
print(x)
print(y)Top-level scope
(3, 4)Object 2 · int3Object 3 · int4The example has finished. Compare the final output with the values and references above.
3 4
What do you expect this program to print?
point = (3, 4)
x, y = point
print(x)
print(y)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 ().
fruits = ("apple", "banana", "cherry")
Here, we created a tuple called fruits that holds three items. The round brackets mean it's a tuple.
(x, y).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')
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
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.
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
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
Because tuples are unchangeable:
fruits[0] = "orange"
TypeError: 'tuple' object does not support item assignment
Python will give you an error if you try to change the content.
Connect what you noticed in the example with the idea below.
A tuple is an immutable sequence. Unpacking binds each name to the corresponding element; a one-element tuple needs a comma.
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.