{"id":"arithmetic-operators","slug":"operators","title":"Arithmetic operators","text":"You have already used `+`, `-`, `*` and `/`. Python has three more arithmetic operators. Here they all are:\n\n| Operator | What it does | Example | Result |\n|:-:|---|---|---|\n| `+` | addition | `7 + 2` | `9` |\n| `-` | subtraction | `7 - 2.5` | `4.5` |\n| `*` | multiplication | `-3 * 4` | `-12` |\n| `/` | division | `7 / 2` | `3.5` |\n| `//` | integer division (rounds down) | `7 // 2` | `3` |\n| `%` | remainder, also called modulo | `7 % 2` | `1` |\n| `**` | power | `2 ** 3` | `8` |\n\n### Integer division and the remainder\n\n`//` divides and rounds the result down to a whole number: it tells you how many times one number fits into another. `%` gives what is left over. Together they answer questions such as \"how many full boxes, and how many items left\":\n\n```pycon\n>>> 17 // 5\n3\n>>> 17 % 5\n2\n```\n\n5 fits into 17 three times, which makes 15, and 2 is left over. Here the same idea turns minutes into hours and minutes:\n\n```python\nminutes = 135\nhours = minutes // 60\nrest = minutes % 60\nprint(f\"{minutes} minutes is {hours} h {rest} min.\")\n```\n\n```output\n135 minutes is 2 h 15 min.\n```\n\nThe remainder is also a quick test for even numbers: an even number divided by 2 leaves nothing, so `14 % 2` is 0, while `15 % 2` is 1. That comes in handy with the conditions at the end of this part.\n\n### Powers\n\n`**` raises a number to a power: `2 ** 3` is 2 * 2 * 2.\n\n```pycon\n>>> 2 ** 3\n8\n>>> 5 ** 2\n25\n>>> 10 ** 6\n1000000\n```\n\nWatch out: in Python, `^` is not a power. It's a different operator that this course doesn't need, so `2 ^ 3` gives a wrong answer without any error message:\n\n```pycon\n>>> 2 ^ 3\n1\n```\n","task":"Fill in the blanks so the program prints how many full weeks and extra days there are in 100 days:\n\n```\n100 days is 14 weeks and 2 days.\n```\n","kind":"fill","seed":"days = 100\nweeks = days [[//]] 7\nleft = days [[%]] 7\nprint(f\"{days} days is {weeks} weeks and {left} days.\")\n","solution":"days = 100\nweeks = days // 7\nleft = days % 7\nprint(f\"{days} days is {weeks} weeks and {left} days.\")\n","hints":["The number of full weeks is how many times 7 fits into 100. The extra days are what's left over.","`//` divides and rounds down, and `%` gives the remainder."],"tests":[{"name":"Output","stdin":"","expect":"100 days is 14 weeks and 2 days.","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"print(9 / 2)\nprint(9 // 2)\nprint(9 % 2)\nprint(3 ** 2)\n","solution":"print(9 / 2)\nprint(9 // 2)\nprint(9 % 2)\nprint(3 ** 2)\n","hints":["`/` gives the exact result as a float, and `//` rounds it down to a whole number.","`%` is what's left over when 2 is taken out of 9 as many times as it fits, and `**` is a power: 3 * 3."],"tests":[{"name":"Output","stdin":"","expect":"4.5\n4\n1\n9","hidden":false}],"require":[],"forbid":[],"lines":["4.5","4","1","9"]},{"task":"An egg carton holds 12 eggs. The variable `eggs` holds the number of eggs collected today. Add lines that calculate how many cartons can be filled and how many eggs are left over, and print this line:\n\n```\n100 eggs: 8 full cartons, 4 eggs left over\n```\n","kind":"code","seed":"eggs = 100\n","solution":"eggs = 100\ncartons = eggs // 12\nleft = eggs % 12\nprint(f\"{eggs} eggs: {cartons} full cartons, {left} eggs left over\")\n","hints":["The number of full cartons is how many times 12 fits into `eggs`.","`eggs // 12` is the number of full cartons and `eggs % 12` the eggs left over.","Print them with an f-string: `f\"{eggs} eggs: {cartons} full cartons, ...\"`."],"tests":[{"name":"Output","stdin":"","expect":"100 eggs: 8 full cartons, 4 eggs left over","hidden":false}],"require":[],"forbid":[{"pattern":"\\b8\\b|\\b4\\b","raw":true,"message":"Let Python calculate the cartons and the eggs left over from `eggs`."}]}]}