{"id":"more-lists-any-type","slug":"any-type","title":"Lists of strings, floats and more","text":"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.\n\n### A list of strings\n\n```python\nguests = [\"Maya\", \"Leo\", \"Iris\"]\nguests.append(\"Omar\")\nprint(guests)\nprint(\"Guests:\", len(guests))\n\nguests.sort()\nfor guest in guests:\n    print(guest)\n```\n\n```output\n['Maya', 'Leo', 'Iris', 'Omar']\nGuests: 4\nIris\nLeo\nMaya\nOmar\n```\n\nWhen 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.\n\nCareful with capital letters: Python compares strings character by character, and every capital letter comes before every small letter. So `\"Zoe\"` comes before `\"adam\"`:\n\n```pycon\n>>> sorted([\"adam\", \"Zoe\", \"bea\"])\n['Zoe', 'adam', 'bea']\n```\n\n### A list of floats\n\nA list of floating point numbers works with `sum`, `min` and `max` just like a list of integers:\n\n```python\nrainfall = [12.5, 0.0, 3.2, 8.8, 0.4]\n\nprint(\"Wettest day:\", max(rainfall))\nmean = sum(rainfall) / len(rainfall)\nprint(f\"Mean: {mean:.2f} mm\")\n```\n\n```output\nWettest day: 12.5\nMean: 4.98 mm\n```\n\n### Mixing types\n\nOne 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:\n\n```python\nmixed = [3, \"lamp\", 19.99]\nmixed.sort()\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    mixed.sort()\n    ~~~~~~~~~~^^\nTypeError: '<' not supported between instances of 'str' and 'int'\n```\n\nThe `<` 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.\n","task":"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:\n\n```\nLongest: 3.15\nMean: 2.50\n```\n","kind":"fill","seed":"jumps = [2.4, 3.15, 1.8, 2.65]\n\nprint(\"Longest:\", [[max]](jumps))\nmean = [[sum]](jumps) / [[len]](jumps)\nprint(f\"Mean: {mean:.2f}\")\n","solution":"jumps = [2.4, 3.15, 1.8, 2.65]\n\nprint(\"Longest:\", max(jumps))\nmean = sum(jumps) / len(jumps)\nprint(f\"Mean: {mean:.2f}\")\n","hints":["Python has a built-in function for each blank. They work on a list of floats just as on a list of integers.","The largest item is `max(jumps)`. The mean is the sum of the items divided by how many there are: `sum(jumps) / len(jumps)`."],"tests":[{"name":"Output","stdin":"","expect":"Longest: 3.15\nMean: 2.50","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"names = [\"eve\", \"Bob\", \"alice\", \"Dan\"]\nnames.sort()\nprint(names[0])\nprint(names[-1])\nprint(len(names[2]))\n","solution":"names = [\"eve\", \"Bob\", \"alice\", \"Dan\"]\nnames.sort()\nprint(names[0])\nprint(names[-1])\nprint(len(names[2]))\n","hints":["`sort` puts the strings in alphabetical order, but every capital letter comes before every small letter.","After sorting, the list is `['Bob', 'Dan', 'alice', 'eve']`.","`names[2]` is `'alice'`, and `len` counts its letters."],"tests":[{"name":"Output","stdin":"","expect":"Bob\neve\n5","hidden":false}],"require":[],"forbid":[],"lines":["Bob","eve","5"]},{"task":"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.\n\nFor 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.\n","kind":"code","seed":"def above_average(values: list) -> list:\n    pass\n\n\nif __name__ == \"__main__\":\n    print(above_average([2.5, 4.0, 1.5, 6.0]))\n","solution":"def above_average(values: list) -> list:\n    mean = sum(values) / len(values)\n    result = []\n    for value in values:\n        if value > mean:\n            result.append(value)\n    return result\n\n\nif __name__ == \"__main__\":\n    print(above_average([2.5, 4.0, 1.5, 6.0]))\n","hints":["First calculate the mean, once, before the loop. Then go through the values and compare each one with it.","Start with an empty list, `result = []`, and `append` each value that is greater than the mean. Return `result` after the loop.","The mean is `sum(values) / len(values)`."],"tests":[{"name":"above_average([2.5, 4.0, 1.5, 6.0])","stdin":"","call":"above_average([2.5, 4.0, 1.5, 6.0])\n","expect":"[4.0, 6.0]","hidden":false,"returns":true},{"name":"above_average([10.0, 10.0])","stdin":"","call":"above_average([10.0, 10.0])\n","expect":"[]","hidden":false,"returns":true},{"name":"above_average([0.5, 9.75, 3.0])","stdin":"","call":"above_average([0.5, 9.75, 3.0])\n","expect":"[9.75]","hidden":true,"returns":true},{"name":"above_average([-1.5, 2.0, 0.25, 7.5, -3.0])","stdin":"","call":"above_average([-1.5, 2.0, 0.25, 7.5, -3.0])\n","expect":"[2.0, 7.5]","hidden":true,"returns":true}],"require":[],"forbid":[]}]}