{"id":"lists-functions","slug":"max-min-sum","title":"max, min, sum and methods versus functions","text":"Python has functions for the most common questions about a list of numbers. **`max`** returns the greatest item, **`min`** the smallest and **`sum`** the sum of all the items:\n\n```python\nsales = [120, 75, 210, 95]\nprint(\"Best day:\", max(sales))\nprint(\"Worst day:\", min(sales))\nprint(\"Total:\", sum(sales))\nprint(\"Average:\", sum(sales) / len(sales))\n```\n\n```output\nBest day: 210\nWorst day: 75\nTotal: 500\nAverage: 125.0\n```\n\nThe average is the sum divided by the number of items. `max` and `min` work for strings too: then they give the alphabetically last and first item.\n\n### Methods and functions\n\nYou now know two kinds of commands that work with lists, and they are written differently:\n\n- **methods** are written after the list, with a dot: `sales.append(80)`, `sales.sort()`, `sales.pop()`. Most of them change the list.\n- **functions** get the list as an argument, in the parentheses: `len(sales)`, `max(sales)`, `sum(sales)`, `sorted(sales)`. They return a value and leave the list as it is.\n\n```python\nsales = [120, 75, 210]\nsales.append(95)\nsales.sort()\nprint(sales)\nprint(len(sales), max(sales), sorted(sales))\n```\n\n```output\n[75, 95, 120, 210]\n4 210 [75, 95, 120, 210]\n```\n\nMixing the two up is a common slip, and Python tells you so. There is no `max` method:\n\n```python\nsales = [120, 75, 210]\nprint(sales.max())\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    print(sales.max())\n          ^^^^^^^^^\nAttributeError: 'list' object has no attribute 'max'\n```\n\nand no `sort` function:\n\n```python\nsales = [120, 75, 210]\nsort(sales)\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    sort(sales)\n    ^^^^\nNameError: name 'sort' is not defined. Did you mean: 'sorted'?\n```\n\nAn `AttributeError` about a `'list' object` means you wrote a function as a method; a `NameError` means you wrote a method as a function.\n\nAn empty list has no greatest or smallest item, so `max([])` and `min([])` stop with a `ValueError`. `sum([])` is 0.\n","task":"Fill in the blanks so the program prints the total, the highest and the lowest of the step counts:\n\n```\nTotal: 31200\nHighest: 9800\nLowest: 3100\n```\n","kind":"fill","seed":"steps = [6500, 9800, 3100, 4600, 7200]\nprint(\"Total:\", [[sum]](steps))\nprint(\"Highest:\", [[max]](steps))\nprint(\"Lowest:\", [[min]](steps))\n","solution":"steps = [6500, 9800, 3100, 4600, 7200]\nprint(\"Total:\", sum(steps))\nprint(\"Highest:\", max(steps))\nprint(\"Lowest:\", min(steps))\n","hints":["Each blank is the name of a function that takes the list as its argument.","`sum` adds up the items, `max` gives the greatest and `min` the smallest."],"tests":[{"name":"Output","stdin":"","expect":"Total: 31200\nHighest: 9800\nLowest: 3100","hidden":false}],"require":[],"forbid":[],"more":[{"task":"Write a program that reads temperatures (whole numbers) until the user types an empty line, and then prints how many there were, the warmest, the coldest and the average:\n\n```\nTemperature: 18\nTemperature: 23\nTemperature: 16\nTemperature:\nReadings: 3\nWarmest: 23\nColdest: 16\nAverage: 19.0\n```\n\nYou can assume the user types at least one temperature.\n","kind":"code","seed":"temperatures = []\n","solution":"temperatures = []\nwhile True:\n    line = input(\"Temperature: \")\n    if line == \"\":\n        break\n    temperatures.append(int(line))\nprint(\"Readings:\", len(temperatures))\nprint(\"Warmest:\", max(temperatures))\nprint(\"Coldest:\", min(temperatures))\nprint(\"Average:\", sum(temperatures) / len(temperatures))\n","hints":["First collect the temperatures in the list, converted with `int`, in a loop that stops on an empty line.","After the loop, `len`, `max` and `min` give the first three answers.","The average is `sum(temperatures) / len(temperatures)`."],"tests":[{"name":"Test 1","stdin":"18\n23\n16\n\n","expect":"Temperature: 18\nTemperature: 23\nTemperature: 16\nTemperature:\nReadings: 3\nWarmest: 23\nColdest: 16\nAverage: 19.0","hidden":false},{"name":"Test 2","stdin":"-5\n2\n0\n-1\n\n","expect":"Temperature: -5\nTemperature: 2\nTemperature: 0\nTemperature: -1\nTemperature:\nReadings: 4\nWarmest: 2\nColdest: -5\nAverage: -1.0","hidden":true},{"name":"Test 3","stdin":"21\n\n","expect":"Temperature: 21\nTemperature:\nReadings: 1\nWarmest: 21\nColdest: 21\nAverage: 21.0","hidden":true}],"require":[],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"values = [3, 9, 1]\nvalues.append(max(values) + 1)\nprint(values)\nprint(sorted(values)[1])\nprint(sum(values) - len(values))\nprint(min(values) == values[2])\n","solution":"values = [3, 9, 1]\nvalues.append(max(values) + 1)\nprint(values)\nprint(sorted(values)[1])\nprint(sum(values) - len(values))\nprint(min(values) == values[2])\n","hints":["`max(values)` is 9, so the method `append` adds 10 to the end.","`sorted(values)` is `[1, 3, 9, 10]`, and `[1]` takes its second item.","The sum of the four items is 23, and the list has 4 items."],"tests":[{"name":"Output","stdin":"","expect":"[3, 9, 1, 10]\n3\n19\nTrue","hidden":false}],"require":[],"forbid":[],"lines":["[3, 9, 1, 10]","3","19","True"]}]}