Order of Operations in Python (Operator Precedence)

Python’s order of operations follows the PEMDAS rule from math class: parentheses first, then exponents (**), then multiplication and division, then addition and subtraction, working left to right within each level.

2 + 3 * 4      # 14, not 20
(2 + 3) * 4    # 20

I checked every expression on Python 3.12.5, so the results below are exactly what Python prints, including a few that surprise people.

Below you’ll find PEMDAS in Python, the exponent quirks, floor division and modulo, how and, or and not rank, a full precedence table, and the gotchas worth memorizing.

PEMDAS: the order of operations in Python

You’ll find multiplication and division run before addition and subtraction. Operators on the same level, like - and -, run left to right:

print(2 + 3 * 4)          # 14: multiplication first
print((2 + 3) * 4)        # 20: parentheses first
print(10 - 4 - 3)         # 3: same level goes left to right
print(100 / 10 / 5)       # 2.0
print(2 + 10 / 4 * 2)     # 7.0: / and * left to right, then +

subtotal, tax_rate, shipping = 80.00, 0.0825, 5.99
print(f"wrong: ${subtotal + subtotal * tax_rate + shipping * 2:.2f}")    # ships twice
print(f"right: ${(subtotal * (1 + tax_rate) + shipping):.2f}")

Output:

14
20
3
2.0
7.0
wrong: $98.58
right: $92.59
Command Prompt output of Python order of operations examples showing multiplication before addition and parentheses changing results
The last two lines show a real bug: without parentheses, shipping gets doubled instead of the subtotal getting taxed.

When a formula involves money, I add parentheses even where they aren’t strictly needed. They cost nothing and make the intent obvious.

Exponent precedence: why -2 ** 2 is -4

** binds tighter than a minus sign on its left, and it groups from right to left. Both rules can catch you out:

print(2 ** 3 ** 2)        # 512: ** groups right to left, 2 ** 9
print((2 ** 3) ** 2)      # 64
print(-2 ** 2)            # -4: the minus applies after **
print((-2) ** 2)          # 4
print(2 ** -1)            # 0.5: a unary minus on the right is fine

Output:

512
64
-4
4
0.5
Command Prompt output of Python exponent precedence: 2 ** 3 ** 2 is 512 and -2 ** 2 is -4
2 ** 3 ** 2 means 2 ** 9. To square a negative number, wrap it: (-2) ** 2.

More on powers and roots in exponents in Python.

Floor division and modulo precedence

// and % sit on the same level as * and /, so they’re evaluated left to right with them. Watch what happens when you use negative numbers:

print(7 // 2 * 2)         # 6: // and * share a level, left to right
print(7 % 3 * 2)          # 2
print(-7 // 2)            # -4: floor division rounds down, not toward zero
print(-7 % 3)             # 2
print(17 - 17 // 5 * 5)   # 2, same as 17 % 5

Output:

6
2
-4
2
2

If -7 // 2 giving -4 surprises you, see normal division vs floor division.

Order of and, or and not in Python

not runs first, then and, then or. Comparisons all rank above them, and they chain, so 1 < age < 21 works like it does in math:

age, member = 17, True

print(age >= 18 or member and age > 15)       # or (and ...) -> True
print((age >= 18 or member) and age > 15)     # True
print(not age > 18 and member)                # (not ...) and ... -> True
print(1 < age < 21)                           # chained: 1 < age and age < 21
print(1 == 1 in [1])                          # chained: (1 == 1) and (1 in [1])

flags = 6
print(flags & 2 == 2)       # & binds tighter than ==, so this is (6 & 2) == 2
print(flags & (2 == 2))     # 6 & True -> 0

Output:

True
True
True
True
True
True
0
Command Prompt output of Python operator precedence for and, or, not, chained comparisons and the bitwise and operator
flags & 2 == 2 is True because & runs before ==, but adding the wrong parentheses gives 0.

There’s a whole guide on and, or and not in Python, including short-circuiting.

Python operator precedence table

Here’s the full order you can bookmark, from the operators that bind tightest to the loosest. Operators in the same row share a level:

PrecedenceOperatorsDescription
1 (highest)( ), [ ], { }Grouping, displays
2x[i], x(...), x.attrSubscription, calls, attributes
3await xAwait expression
4**Exponent (right to left)
5+x, -x, ~xUnary plus, minus, bitwise not
6*, /, //, %, @Multiply, divide, floor divide, modulo
7+, -Add, subtract
8<<, >>Bit shifts
9&Bitwise AND
10^Bitwise XOR
11|Bitwise OR
12in, not in, is, is not, <, <=, >, >=, !=, ==Comparisons (they chain)
13notBoolean NOT
14andBoolean AND
15orBoolean OR
16x if c else yConditional expression
17lambdaLambda
18 (lowest):=Assignment expression (walrus)

Which operator precedence mistakes are most common?

The conditional expression and the walrus operator both sit near the bottom, so they swallow more than you might expect. Your string expressions follow the same rules too:

price = 120.00
label = "free shipping" if price > 100 else "standard" + " shipping"
print(label)                                       # the + only belongs to the else part

items = [4.50, 2.75, 3.25]
if (count := len(items)) > 2:                      # := needs parentheses here
    print(count, "items")

s = "Austin"
print("Hi " + s * 2)                               # * before +
print(not "" == "x")                               # not (... == ...) -> True

Output:

free shipping
3 items
Hi AustinAustin
True

In the first line, + " shipping" only attaches to the else branch. For more on conditions, see if else in Python and does not equal in Python.

Related operator guides worth reading:

Frequently asked questions

What is the order of operations in Python?

Parentheses, exponents, multiplication and division (including // and %), then addition and subtraction, left to right within each level.

Does Python follow PEMDAS?

Yes for arithmetic. Python then adds more levels for bitwise, comparison and boolean operators.

Why is -2 ** 2 negative in Python?

Exponentiation binds tighter than the unary minus on its left, so it’s read as -(2 ** 2).

Is ** left or right associative?

Right associative: 2 ** 3 ** 2 is 2 ** (3 ** 2), which is 512.

Which comes first, and or or?

and. So a or b and c means a or (b and c). not comes before both.

Where is the official precedence table?

See the operator precedence section of the Python language reference.