Which part first?
Know which part of a sum happens first, and use brackets to choose. About 7 minutes.
Multiply before you add
At a shop you buy 3 pencils at 4 rupees each, plus one rubber at 2 rupees. You'd say the bill is 2 plus 3 times 4, which is 14. You would not add 2 and 3 first.
The computer follows the same school rule. Multiplying and dividing happen before adding and subtracting, whatever order you wrote them in. Where the priority is the same, it goes from left to right.
bill = 2 + 3 * 4 # 14, not 20
The computer scans the whole line and spots the times sign first. Times goes before plus.
Brackets decide
What if you do want the adding first? Put it in brackets. Whatever is inside brackets happens before anything outside.
This matters when you average numbers. To find the average of three numbers you add them up and then divide by 3. Without brackets, the computer divides only the last number by 3, and then adds. If in doubt, add brackets: they never hurt.
wrong = a + b + c // 3 right = (a + b + c) // 3
What does 10 - 2 * 3 work out to?
- A4
- B24
- C−8
Show the answer
4. Times goes first: 2 times 3 is 6, and 10 minus 6 is 4.
Average of three
Return the average of three whole numbers, rounded down to a whole number: add them up, then divide by 3.
a = 4, b = 5, c = 9 → 6
0 ≤ a, b, c ≤ 1,000,000