13.2. Tuple Packing¶
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.
Check your understanding
- print(julia['city'])
- julia is a tuple, not a dictionary; indexes must be integers.
- print(julia[-1])
- [-1] picks out the last item in the sequence.
- print(julia(-1))
- Index into tuples using square brackets. julia(-1) will try to treat julia as a function call, with -1 as the parameter value.
- print(julia(6))
- Index into tuples using square brackets. julia(-1) will try to treat julia as a function call, with -1 as the parameter value.
- print(julia[7])
- Indexing starts at 0. You want the seventh item, which is julia[6]
Which of the following statements will output Atlanta, Georgia
2. Create a tuple called practice
that has four elements: ‘y’, ‘h’, ‘z’, and ‘x’.
3. Create a tuple named tup1
that has three elements: ‘a’, ‘b’, and ‘c’.
4. Provided is a list of tuples. Create another list called t_check
that contains the third element of every tuple.
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
.
You have attempted of activities on this page