TExcelTAD
Home › Basic Math & Pre-Algebra › Order of Operations

Order of Operations

What is 2+3×42 + 3 \times 4? 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+3)×4=5×4=20(2 + 3) \times 4 = 5 \times 4 = 20
2 Exponents (powers and roots) 2×32=2×9=182 \times 3^2 = 2 \times 9 = 18
3 Multiplication and division, left to right 20÷4×5=5×5=2520 \div 4 \times 5 = 5 \times 5 = 25
4 Addition and subtraction, left to right 10−4+3=6+3=910 - 4 + 3 = 6 + 3 = 9

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. 20÷4×520 \div 4 \times 5 is 25, not 1.

So 2+3×4=2+12=142 + 3 \times 4 = 2 + 12 = 14. Multiplication outranks addition.

Worked examples

18−2×(7−4)2÷6=18−2×32÷6brackets: 7−4=3=18−2×9÷6exponent: 32=9=18−18÷6multiply/divide left to right: 2×9=18=18−3then 18÷6=3=15subtract\begin{aligned} &18 - 2 \times (7 - 4)^2 \div 6 \\ &= 18 - 2 \times 3^2 \div 6 && \text{brackets: } 7 - 4 = 3 \\ &= 18 - 2 \times 9 \div 6 && \text{exponent: } 3^2 = 9 \\ &= 18 - 18 \div 6 && \text{multiply/divide left to right: } 2 \times 9 = 18 \\ &= 18 - 3 && \text{then } 18 \div 6 = 3 \\ &= 15 && \text{subtract} \end{aligned}

Nested brackets are worked from the inside out:

3×[10−(2+1)×2]=3×[10−3×2]=3×[10−6]=3×4=12\begin{aligned} &3 \times [10 - (2 + 1) \times 2] \\ &= 3 \times [10 - 3 \times 2] \\ &= 3 \times [10 - 6] \\ &= 3 \times 4 \\ &= 12 \end{aligned}

A fraction bar acts like brackets around the top and the bottom: 8+45−2\frac{8 + 4}{5 - 2} means 12÷3=412 \div 3 = 4.

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:

python · try it yourself
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:

python · try it yourself
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 (32)=29=512(3^2) = 2^9 = 512, not (23)2=82=64(2^3)^2 = 8^2 = 64. 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 / 2 computes a+(b÷2)a + (b \div 2). The correct code is (a + b) / 2. With a=80a = 80 and b=90b = 90, 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 * 4 into a tree that encodes precedence; algorithms such as the shunting-yard algorithm do exactly this.
  • Viral puzzles like 8÷2(2+2)8 \div 2(2 + 2) 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) or 8 / (2 * (2 + 2)) (= 1.0) and the ambiguity disappears.
python · try it yourself
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) 6+4×56 + 4 \times 5 (b) 30÷5×330 \div 5 \times 3 (c) 2×42−102 \times 4^2 - 10 (d) (9−3)2÷(1+2)(9 - 3)^2 \div (1 + 2)

Answer

(a) 26 (b) 18 (c) 22 (d) 12

Solution
(a)4×5=20, then 6+20=26(b)left to right: 30÷5=6, then 6×3=18(c)42=16, 2×16=32, 32−10=22(d)brackets: 6 and 3; 62=36; 36÷3=12\begin{aligned} &\text{(a)}\quad 4 \times 5 = 20,\ \text{then } 6 + 20 = 26 \\ &\text{(b)}\quad \text{left to right: } 30 \div 5 = 6,\ \text{then } 6 \times 3 = 18 \\ &\text{(c)}\quad 4^2 = 16,\ 2 \times 16 = 32,\ 32 - 10 = 22 \\ &\text{(d)}\quad \text{brackets: } 6 \text{ and } 3;\ 6^2 = 36;\ 36 \div 3 = 12 \end{aligned}
python · try it yourself
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 50−2×[3+(8−2×3)2]50 - 2 \times [3 + (8 - 2 \times 3)^2].

Answer

36

Solution
innermost:8−2×3=8−6=2square:22=4square brackets:3+4=7then:50−2×7=50−14=36\begin{aligned} \text{innermost:} &\quad 8 - 2 \times 3 = 8 - 6 = 2 \\ \text{square:} &\quad 2^2 = 4 \\ \text{square brackets:} &\quad 3 + 4 = 7 \\ \text{then:} &\quad 50 - 2 \times 7 = 50 - 14 = 36 \end{aligned}

Exercise 3: Place the brackets

Add one pair of brackets to make each statement true: (a) 3+5×2=163 + 5 \times 2 = 16 (b) 24÷4−2=1224 \div 4 - 2 = 12 (c) 2×3+42=982 \times 3 + 4^2 = 98

Answer

(a) (3+5)×2(3 + 5) \times 2 (b) 24÷(4−2)24 \div (4 - 2) (c) 2×(3+4)22 \times (3 + 4)^2

Solution

(a) 8×2=168 \times 2 = 16. (b) 24÷2=1224 \div 2 = 12. (c) 72=497^2 = 49 and 2×49=982 \times 49 = 98.

python · try it yourself
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 C=(F−32)×5÷9C = (F - 32) \times 5 \div 9, but prints the wrong value. Find the bug and fix it.

python · try it yourself
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 32×5÷9≈17.7832 \times 5 \div 9 \approx 17.78 first and subtracts that from 77. The formula subtracts 32 first:

python · try it yourself
f = 77
c = (f - 32) * 5 / 9
print(c)
25.0

Test yourself

What is 12−6÷3×212 - 6 \div 3 \times 2?

  1. 2
  2. 8
  3. 1
  4. 11

What does Python print for 2 ** 2 ** 3?

  1. 64
  2. 256
  3. 12
  4. 4096

Which line correctly computes the average of xx and yy?

  1. x + y / 2
  2. x + (y / 2)
  3. x / 2 + y
  4. (x + y) / 2