{"id":"arithmetic-precedence","slug":"order-of-operations","title":"Order of operations","text":"A calculation is made of **operands**, the values, and **operators**, such as `+` and `*`. When a calculation has several operators, Python works them out in the order you know from mathematics:\n\n1. whatever is in parentheses\n2. powers, `**`\n3. multiplication and division: `*`, `/`, `//` and `%`\n4. addition and subtraction: `+` and `-`\n\nOperators on the same level are worked out from left to right. Powers are the exception: `2 ** 3 ** 2` is worked out from the right, as `2 ** 9`.\n\n```python\nprint(2 + 3 * 4)\nprint((2 + 3) * 4)\nprint(2 * 3 ** 2)\nprint(20 - 8 - 2)\nprint(20 - (8 - 2))\n```\n\n```output\n14\n20\n18\n10\n14\n```\n\nIn `2 * 3 ** 2`, the power comes first: `3 ** 2` is 9, and 2 * 9 is 18. In `20 - 8 - 2`, Python goes from left to right: 20 - 8 is 12, and 12 - 2 is 10. The parentheses in the last line make Python do `8 - 2` first.\n\n### A classic mistake\n\nThe mean of two numbers is their sum divided by two. Forget the parentheses, and only the second number is divided:\n\n```python\nfirst = 8\nsecond = 6\nwrong = first + second / 2\nright = (first + second) / 2\nprint(wrong)\nprint(right)\n```\n\n```output\n11.0\n7.0\n```\n\nPython gave no error: `first + second / 2` is a perfectly good calculation, just not the one that was meant. Its result is 8 + 3.0, which is 11.0. Python always does what the code says, so check that the order of the calculation is what you want.\n\n### Parentheses in a longer formula\n\nHere the price of a product drops by 25 percent, and then shipping is added:\n\n```python\nprice = 80\ndiscount = 25\nshipping = 5\ntotal = price * (100 - discount) / 100 + shipping\nprint(total)\n```\n\n```output\n65.0\n```\n\nThe parentheses make Python work out the share of the price you pay, 75 percent, first. Then `*` and `/` go from left to right: 80 * 75 is 6000, and 6000 / 100 is 60.0. The addition comes last. When you aren't sure of the order, add parentheses: they never hurt, and they make the formula easier to read.\n","task":"Fill in the blanks with parentheses so the program prints 30, not 22.\n\n```\n30\n```\n","kind":"fill","seed":"print([[(]]4 + 6[[)]] * 3)\n","solution":"print((4 + 6) * 3)\n","hints":["Without parentheses, Python multiplies 6 by 3 first.","Put `(` before the 4 and `)` after the 6, so the addition is worked out first."],"tests":[{"name":"Output","stdin":"","expect":"30","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"print(1 + 2 * 3)\nprint((1 + 2) * 3)\nprint(10 - 2 ** 3)\nprint(12 / 2 * 3)\n","solution":"print(1 + 2 * 3)\nprint((1 + 2) * 3)\nprint(10 - 2 ** 3)\nprint(12 / 2 * 3)\n","hints":["Parentheses first, then powers, then `*` and `/`, then `+` and `-`.","`2 ** 3` is 8. In the last line, `/` and `*` are on the same level, so Python goes from left to right, and `/` makes the result a float."],"tests":[{"name":"Output","stdin":"","expect":"7\n9\n2\n18.0","hidden":false}],"require":[],"forbid":[],"lines":["7","9","2","18.0"]},{"task":"The program should print the mean of three days' practice hours, but it prints a much bigger number. Fix the calculation, so the program prints:\n\n```\nMean: 7.0\n```\n","kind":"code","seed":"monday = 4\ntuesday = 7\nwednesday = 10\nmean = monday + tuesday + wednesday / 3\nprint(f\"Mean: {mean}\")\n","solution":"monday = 4\ntuesday = 7\nwednesday = 10\nmean = (monday + tuesday + wednesday) / 3\nprint(f\"Mean: {mean}\")\n","hints":["Division comes before addition. Which number is divided by 3 now?","Put the sum in parentheses, so all three numbers are added before the division."],"tests":[{"name":"Output","stdin":"","expect":"Mean: 7.0","hidden":false}],"require":[],"forbid":[]}]}