13.2 Tuple Packing
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython1 class period4 exercises4 graded tests
13.2 Tuple Packing
Read first in the book: 13.1 Introduction.
Wherever python expects a single value, if multiple expressions are provided, separated by commas, they are automatically packed into a tuple. For example, we can omit the parentheses when assigning a tuple of values to a single variable.
julia = ("Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia")
# or equivalently
julia = "Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia"
print(julia[4])
Check your understanding
Check your understanding: a multiple-choice question in the book.
Exercise 1
2. Create a tuple called practice that has four elements: 'y', 'h', 'z', and 'x'.
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
3. Create a tuple named tup1 that has three elements: 'a', 'b', and 'c'.
Open the
02folder and editmain.py. Run Tests checks it.
Exercise 3
4. Provided is a list of tuples. Create another list called t_check that contains the third element of every tuple.
Open the
03folder and editmain.py. Run Tests checks it.
Exercise 4
5. Below, we have provided a list of tuples. Write a for loop that saves the second element of each tuple into a list called seconds.
Open the
04folder and editmain.py. Run Tests checks it.
For teachers
13.2 Tuple Packing
Students read Foundations of Python Programming's “Tuple Packing” in CS Room and work through its 4 Python coding exercises (4 graded).
- Time: 1 class period
- Tests: 4 in 4 graded exercises, set up for you when you import the lesson
Reading first
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 1 |
02 | Autograded | 1 |
03 | Autograded | 1 |
04 | Autograded | 1 |