{"id":"arithmetic-types","slug":"number-types","title":"Types of results","text":"Every value has a type. The function `type` tells you which:\n\n```pycon\n>>> type(7)\n<class 'int'>\n>>> type(7.0)\n<class 'float'>\n>>> type(\"7\")\n<class 'str'>\n```\n\nDon't mind the word \"class\" for now; the part in quotes is the name of the type. So `7` is an integer, `7.0` is a float and `\"7\"` is a string.\n\n### The type of a result\n\nWhat type a calculation gives depends on its operator and on its operands. Compare these:\n\n```pycon\n>>> 2 + 3\n5\n>>> 2 + 3.0\n5.0\n>>> 6 / 3\n2.0\n>>> 7 // 2\n3\n>>> 7.5 // 2\n3.0\n>>> type(6 / 3)\n<class 'float'>\n```\n\nLook at them in pairs. `2 + 3` is the integer 5, but put the float `3.0` in place of `3` and the answer turns into the float `5.0`, though it's still a whole number. `6 / 3` divides two integers evenly and still gives `2.0`: division with `/` gives a float every time. `7 // 2` is the integer 3, and `7.5 // 2` rounds down as well, to `3.0`, a float because a float went in.\n\nThe type shows in what you print, so choose the operator that gives the output you want:\n\n```python\nminutes = 240\nprint(f\"{minutes / 60} hours\")\nprint(f\"{minutes // 60} hours\")\n```\n\n```output\n4.0 hours\n4 hours\n```\n\nIn a real calculation, floats and integers mix freely. This program works out a car's fuel use per 100 kilometers:\n\n```python\nliters = 42.5\nkilometers = 680\nper_100_km = liters / kilometers * 100\nprint(f\"Fuel used: {per_100_km} liters per 100 km\")\nprint(type(per_100_km))\n```\n\n```output\nFuel used: 6.25 liters per 100 km\n<class 'float'>\n```\n\n### Converting between types\n\nThe name of a type also works as a function that converts a value into that type. You've met `str` already; `int` and `float` work the same way:\n\n```pycon\n>>> int(3.9)\n3\n>>> float(3)\n3.0\n>>> str(3.5)\n'3.5'\n```\n\n`int` doesn't round: it simply drops everything after the decimal point, so `int(3.9)` is 3.\n","task":"The program prints `5.0 minutes`. Fill in the blank with the operator that makes it print `5 minutes`:\n\n```\n5 minutes\n```\n","kind":"fill","seed":"seconds = 300\nminutes = seconds [[//]] 60\nprint(f\"{minutes} minutes\")\n","solution":"seconds = 300\nminutes = seconds // 60\nprint(f\"{minutes} minutes\")\n","hints":["`/` always gives a float, and a float is printed with a decimal point.","Integer division, `//`, gives an integer when both numbers are integers."],"tests":[{"name":"Output","stdin":"","expect":"5 minutes","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"print(type(10))\nprint(type(10 / 2))\nprint(type(\"10\"))\nprint(10 // 4)\nprint(int(7.8))\n","solution":"print(type(10))\nprint(type(10 / 2))\nprint(type(\"10\"))\nprint(10 // 4)\nprint(int(7.8))\n","hints":["`type` prints the type as `<class '...'>`. `/` always gives a float, and quotes make a string.","`10 // 4` rounds down, and `int` drops the decimals without rounding."],"tests":[{"name":"Output","stdin":"","expect":"<class 'int'>\n<class 'float'>\n<class 'str'>\n2\n7","hidden":false}],"require":[],"forbid":[],"lines":["<class 'int'>","<class 'float'>","<class 'str'>","2","7"]},{"task":"Three pizzas of 8 slices each are shared by 5 friends. Add lines that print how many slices each friend would get if the slices could be cut exactly, how many whole slices each one gets, and how many slices are left over. Calculate them from the variables:\n\n```\nExactly: 4.8 slices each\nWhole slices: 4 each\nLeft over: 4\n```\n","kind":"code","seed":"pizzas = 3\nslices = pizzas * 8\nfriends = 5\n","solution":"pizzas = 3\nslices = pizzas * 8\nfriends = 5\nprint(f\"Exactly: {slices / friends} slices each\")\nprint(f\"Whole slices: {slices // friends} each\")\nprint(f\"Left over: {slices % friends}\")\n","hints":["Each line divides `slices` by `friends` with a different operator: `/`, `//` and `%`.","`/` gives the exact result as a float, `//` the whole slices and `%` the slices left over.","For example: `print(f\"Whole slices: {slices // friends} each\")`."],"tests":[{"name":"Output","stdin":"","expect":"Exactly: 4.8 slices each\nWhole slices: 4 each\nLeft over: 4","hidden":false}],"require":[],"forbid":[{"pattern":"4\\.8|\\b4\\b","raw":true,"message":"Let Python calculate the results from the variables."}]}]}