Summer Deals - hours only!Up to 80% off on all courses and bundles.-Close
Recap
Tuples as return types in functions
4. Unpacking tuples returned from functions
Tuples as records
List of tuples
Summary

Instruction

Great! When we invoke a function which returns a tuple, we typically unpack the tuple into multiple variables and use them in further calculations. Take a look:

def count_spaces_commas_stops(text):
  spaces, commas, stops = 0, 0, 0 
  for letter in text:
    if letter == ' ':
      spaces += 1
    elif letter == ',':
      commas += 1
    elif letter == '.':
      stops += 1
  return (spaces, commas, stops)

input_text = 'In this example text, there are some commas, spaces and full stops. However, the text is not long.'
spaces, commas, stops = count_spaces_commas_stops(input_text)
print('There are', spaces, 'spaces and', stops, 'full stops in the text.')

We unpacked the result of the get_min_max_mean() invocation into three variables: min, max, and mean. We then used min and max to calculate the difference between the smallest and largest numbers and presented the user with the result.

Exercise

Unpack the result of the get_min_max_mean() invocation into three variables: min, max, and mean. Then use min and max to calculate the difference between the smallest and largest values and print the following sentence:

The difference between the min and max values is {}

Stuck? Here's a hint!

Unpack the tuple like so:

min, max, mean = get_min_max_mean(integers_example)