Python Arena
Step 1 of 5

Lists of strings, floats and more

So far most of your lists have held whole numbers. A list can hold values of any type: strings, floating point numbers, Booleans, even other lists (step 3 of this module shows those). Everything you know about lists works the same way, whatever is inside.

A list of strings

guests = ["Maya", "Leo", "Iris"]
guests.append("Omar")
print(guests)
print("Guests:", len(guests))

guests.sort()
for guest in guests:
    print(guest)
Output
['Maya', 'Leo', 'Iris', 'Omar']
Guests: 4
Iris
Leo
Maya
Omar

When Python prints a whole list, it shows each string in quotes, so you can see where one ends and the next begins. sort puts strings in alphabetical order.

Careful with capital letters: Python compares strings character by character, and every capital letter comes before every small letter. So "Zoe" comes before "adam":

In the Python interpreter
>>> sorted(["adam", "Zoe", "bea"])
['Zoe', 'adam', 'bea']

A list of floats

A list of floating point numbers works with sum, min and max just like a list of integers:

rainfall = [12.5, 0.0, 3.2, 8.8, 0.4]

print("Wettest day:", max(rainfall))
mean = sum(rainfall) / len(rainfall)
print(f"Mean: {mean:.2f} mm")
Output
Wettest day: 12.5
Mean: 4.98 mm

Mixing types

One list can even hold values of different types, such as a product's name, the number in stock and the price: ["lamp", 3, 19.99]. That's allowed, but some things stop working. To sort a list, Python compares its items with each other, and it can't say whether a number is smaller than a string:

mixed = [3, "lamp", 19.99]
mixed.sort()
Python stops with
Traceback (most recent call last):
  File "main.py", line 2, in <module>
    mixed.sort()
    ~~~~~~~~~~^^
TypeError: '<' not supported between instances of 'str' and 'int'

The < in the message is the comparison that sort tried. Most lists hold one type of value, and step 3 shows when a mixed list makes sense.

Your task

Challenge 1 of 3

A frog's jumps are stored in meters. Fill in the blanks so the program prints the longest jump and the mean of the jumps:

Longest: 3.15
Mean: 2.50

Challenge 2

What does this program print? Type each line it prints.

Challenge 3

Write a function above_average(values) that takes a list of floats and returns a new list with the values that are greater than the mean of all the values, in their original order.

For example, the mean of [2.5, 4.0, 1.5, 6.0] is 3.5, so above_average([2.5, 4.0, 1.5, 6.0]) returns [4.0, 6.0]. When no value is greater than the mean, the function returns an empty list.