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 could have omitted the parentheses when first assigning a tuple to the variable julia.

julia = ("Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia")
# or equivalently
julia = "Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia"

Check your understanding

    rec-5-1: Which of the following statements will output Atlanta, Georgia
  • 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]
Next Section - Tuples as Return Values