Order of Operations
What is ? If you work left to right you get 20; if you multiply first you get 14. Only one can be right, so mathematicians agreed on an order of operations: a fixed ranking that says which part of an expression is computed first. Every calculator, spreadsheet and programming language follows the same idea.
The rule
Work through an expression in this order:
| Step | What | Examples |
|---|---|---|
| 1 | Brackets (parentheses), innermost first | |
| 2 | Exponents (powers and roots) | |
| 3 | Multiplication and division, left to right | |
| 4 | Addition and subtraction, left to right |
English-speaking schools remember it as PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction) or BODMAS (Brackets, Orders, Division/Multiplication, Addition/Subtraction). Both say the same thing.
Note: The acronyms mislead people into thinking multiplication comes before division, or addition before subtraction. It doesn't: they are equal partners, done left to right as they appear. is 25, not 1.
So . Multiplication outranks addition.
Worked examples
Nested brackets are worked from the inside out:
A fraction bar acts like brackets around the top and the bottom: means .
Python follows the same rules
Python ranks its operators the same way, so you can type the expression exactly as written, using ( ) for every kind of bracket:
print(2 + 3 * 4)
print((2 + 3) * 4)
print(18 - 2 * (7 - 4) ** 2 / 6)
print(3 * (10 - (2 + 1) * 2))
print(20 / 4 * 5, 20 / (4 * 5))
print(10 - 4 + 3, 10 - (4 + 3))14
20
15.0
12
25.0 1.0
9 3
The third answer is 15.0 rather than 15 only because / always produces a float.
Python's precedence, from highest to lowest, for the operators in this course:
| Precedence | Operators |
|---|---|
| highest | ( ) brackets |
** power (groups right to left) |
|
unary -x |
|
* / // % (left to right) |
|
| lowest | + - (left to right) |
Two details deserve a closer look:
print(-2 ** 2) # power before the minus sign: -(2**2)
print(2 ** 3 ** 2) # powers group right to left: 2 ** 9
print((2 ** 3) ** 2)
print(7 + 10 % 4) # % ranks with * and /-4
512
64
9
Powers stack from the top down: 2 ** 3 ** 2 means 2 raised to the power , not . That matches mathematical writing, where the exponent of an exponent is computed first.
When in doubt, add brackets
Brackets that don't change anything are never wrong, and they make your intention obvious to the reader: (a * b) + c is no slower than a * b + c. For powers with negative numbers, brackets are essential: (-2) ** 2 is 4, -2 ** 2 is −4.
In software
- The classic average bug:
average = a + b / 2computes . The correct code is(a + b) / 2. With and , the bug gives 125 instead of 85, and nothing crashes, so it is easy to miss. - Spreadsheets (
=A1+B1*C1) and calculator apps follow the same ranking; cheap four-function calculators that compute strictly left to right are the exception. - Parsers and compilers turn text like
2 + 3 * 4into a tree that encodes precedence; algorithms such as the shunting-yard algorithm do exactly this. - Viral puzzles like are ambiguous by design: some conventions give implied multiplication higher rank. In code there is no implied multiplication, so you must write
8 / 2 * (2 + 2)(= 16.0) or8 / (2 * (2 + 2))(= 1.0) and the ambiguity disappears.
a, b = 80, 90
print("buggy:", a + b / 2)
print("right:", (a + b) / 2)buggy: 125.0
right: 85.0
Exercises
Exercise 1: Evaluate by hand
(a) (b) (c) (d)
Answer
(a) 26 (b) 18 (c) 22 (d) 12
Solution
print(6 + 4 * 5, 30 / 5 * 3, 2 * 4 ** 2 - 10, (9 - 3) ** 2 / (1 + 2))26 18.0 22 12.0
Exercise 2: Nested brackets
Evaluate .
Answer
36
Solution
Exercise 3: Place the brackets
Add one pair of brackets to make each statement true: (a) (b) (c)
Answer
(a) (b) (c)
Solution
(a) . (b) . (c) and .
print((3 + 5) * 2, 24 / (4 - 2), 2 * (3 + 4) ** 2)16 12.0 98
Exercise 4: Fix the formula
This code should convert 77 °F to Celsius with , but prints the wrong value. Find the bug and fix it.
f = 77
c = f - 32 * 5 / 9
print(c)59.22222222222222
Answer
The subtraction needs brackets: (f - 32) * 5 / 9 gives 25.0 °C.
Solution
Without brackets Python computes first and subtracts that from 77. The formula subtracts 32 first:
f = 77
c = (f - 32) * 5 / 9
print(c)25.0
Test yourself
What is ?
- 2
- 8
- 1
- 11
What does Python print for 2 ** 2 ** 3?
- 64
- 256
- 12
- 4096
Which line correctly computes the average of and ?
- x + y / 2
- x + (y / 2)
- x / 2 + y
- (x + y) / 2