{"id":"variables-floats","slug":"floats","title":"Floating point numbers","text":"Whole numbers aren't enough for prices, measurements or averages. A number with a decimal point, such as `2.5`, `-0.75` or `3.0`, is a **floating point number**, or a **float** for short. Python's name for the type is `float`.\n\nFloats work in calculations just like integers do. This program works out the size of a rug, in meters:\n\n```python\nlength = 2.5\nwidth = 1.25\nprint(f\"Area: {length * width} square meters\")\nprint(f\"Edge all around: {2 * (length + width)} meters\")\n```\n\n```output\nArea: 3.125 square meters\nEdge all around: 7.5 meters\n```\n\nThe parentheses matter. Multiplication comes before addition, so without them Python would work out `2 * length` first and then add `width` to it, once instead of twice.\n\n### Division gives a float\n\nThe result of `/` is always a float, even when the numbers divide evenly:\n\n```pycon\n>>> 7 / 2\n3.5\n>>> 6 / 3\n2.0\n>>> 1.5 + 1.5\n3.0\n```\n\n`2.0` is the same amount as `2`, but it's a float, so Python shows it with a decimal point. The next module looks at the types of results more closely.\n\n### A point, not a comma\n\nPython writes decimals with a point. A comma separates values, so `print(2,5)` prints two numbers, not two and a half:\n\n```python\nprint(2.5)\nprint(2,5)\n```\n\n```output\n2.5\n2 5\n```\n\n### Floats aren't always exact\n\nA computer stores a float in binary with a fixed number of digits, so most decimal fractions are stored as a very close approximation. Usually you never notice, but now and then it shows in the last digit:\n\n```pycon\n>>> 0.1 + 0.2\n0.30000000000000004\n```\n\nThat isn't a mistake in your program: almost every programming language does the same. A later module shows how to round numbers when you print them.\n","task":"Fill in the blank so the program prints the average of the two scores:\n\n```\nAverage: 8.25\n```\n","kind":"fill","seed":"first = 7.5\nsecond = 9.0\naverage = (first + second) / [[2]]\nprint(f\"Average: {average}\")\n","solution":"first = 7.5\nsecond = 9.0\naverage = (first + second) / 2\nprint(f\"Average: {average}\")\n","hints":["The average of two numbers is their sum divided by how many numbers there are.","There are two scores, so divide by `2`."],"tests":[{"name":"Output","stdin":"","expect":"Average: 8.25","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"print(9 / 2)\nprint(10 / 5)\nprint(2.5 * 2)\nprint(0.5 + 1)\n","solution":"print(9 / 2)\nprint(10 / 5)\nprint(2.5 * 2)\nprint(0.5 + 1)\n","hints":["`/` always gives a float, even when the division is even: a float is shown with a decimal point.","When a float takes part in a calculation, the result is a float too, so `2.5 * 2` is shown as `5.0`."],"tests":[{"name":"Output","stdin":"","expect":"4.5\n2.0\n5.0\n1.5","hidden":false}],"require":[],"forbid":[],"lines":["4.5","2.0","5.0","1.5"]},{"task":"A runner's three lap times, in seconds, are in variables. Add lines that calculate the total time and the mean lap time from the variables and print them with f-strings:\n\n```\nTotal: 187.5 seconds\nMean: 62.5 seconds\n```\n","kind":"code","seed":"lap1 = 61.25\nlap2 = 63.5\nlap3 = 62.75\n","solution":"lap1 = 61.25\nlap2 = 63.5\nlap3 = 62.75\ntotal = lap1 + lap2 + lap3\nmean = total / 3\nprint(f\"Total: {total} seconds\")\nprint(f\"Mean: {mean} seconds\")\n","hints":["Store the sum of the three laps in a variable, such as `total`.","The mean is the total divided by the number of laps, 3.","Print each result with an f-string: `print(f\"Total: {total} seconds\")`."],"tests":[{"name":"Output","stdin":"","expect":"Total: 187.5 seconds\nMean: 62.5 seconds","hidden":false}],"require":[],"forbid":[{"pattern":"187\\.5|\\b62\\.5\\b","raw":true,"message":"Let Python calculate the results from the variables instead of typing them."}]}]}