Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.

Returning Several Values

~9 mincode completion

A function can hand back more than one thing by separating values with commas. What comes back is a tuple:

def stats(numbers):
    return min(numbers), max(numbers)

low, high = stats([3, 9, 1])     # low = 1, high = 9
both = stats([3, 9, 1])          # both = (1, 9)

Pulling the values into separate names like low, high = ... is called unpacking. The number of names has to match the number of values or you get a ValueError.

A tuple looks like a list but cannot be changed after it is made. That is exactly what you want for "here are the two things I computed".

Useful built-ins for this problem: min(), max(), sum(), len().

Your task:

Write summarise(numbers) that returns three values in this order: the smallest, the largest, and the mean.

[1, 2, 3, 4] returns (1, 4, 2.5). Assume the list is not empty.

Example Tests

Smallest, largest and mean of four numbers, in that order

Input: {"numbers":[1,2,3,4]}

Expected: [1,4,2.5]

A single item is its own min, max and mean

Input: {"numbers":[5]}

Expected: [5,5,5]

Negative values are handled correctly

Input: {"numbers":[-4,0,4]}

Expected: [-4,4,0]

Python
def summarise(numbers):
    """
    Describe a list of numbers in three figures.

    Args:
        numbers: a non-empty list of numbers

    Returns:
        A tuple (smallest, largest, mean).
    """
    # YOUR CODE HERE
    pass

Run your code to see results

⌘↵ runs against the visible tests

Loading docs…