OperatorsBeginner7 min17 / 63

Operator Precedence

Learn the order Python uses to evaluate math and logic expressions, and how parentheses keep your code clear and correct.

Imagine you write 2 + 3 * 4 in Python. What do you get — 20 or 14?

If you guessed 20 (adding first, then multiplying), you're not alone — but Python says 14. That's because Python has a strict set of rules called operator precedence that decides which operation runs first. Understanding these rules means your code will always do what you intend it to do.

See it in action

Visual walkthrough1 / 5
1

Python Has a VIP Queue

When Python sees 2 + 3 * 4, it doesn't just read left to right — it has priority rules that decide which operator goes first. Get this wrong and your math will silently lie to you.

These rules are called operator precedence — the same idea as PEMDAS from school math.

#A Surprising Example

Multiplication happens before addition, just like in school math.
result = 2 + 3 * 4
print(result)

Python evaluated 3 * 4 first (getting 12), then added 2, giving 14. This is not a bug — it matches the same rules you learned in school under the name PEMDAS (or BODMAS in some countries).

Think of it like

Think of it like a queue at a coffee shop

Some customers get served before others based on their ticket priority — gold ticket first, then silver, then regular. Python's operators have their own "ticket priority". The one with the highest priority gets evaluated first.

#The Precedence Ladder (Highest to Lowest)

Here is the order Python uses, from highest priority (evaluated first) to lowest priority (evaluated last):

  • ** — exponentiation (power)
  • Unary - and + — like the minus in -5
  • *, /, //, % — multiplication, division, floor division, modulo
  • +, - — addition and subtraction
  • ==, !=, <, >, <=, >= — comparisons
  • not — logical NOT
  • and — logical AND
  • or — logical OR

When two operators share the same priority level, Python works left to right (except for **, which goes right to left).

#Exponentiation Comes First

** has the highest priority, so 3**2 (=9) is calculated before anything else.
print(2 + 3 ** 2)
print(2 * 3 ** 2)

#Multiplication Before Addition

* and / always win over + and -.
print(10 - 2 * 3)
print(10 / 2 + 3)

#Same-Level Operators Go Left to Right

When priorities are equal, Python evaluates left to right: (20-5)-3 and (20/4)/2.
print(20 - 5 - 3)
print(20 / 4 / 2)
Common mistake

Exponentiation is right-to-left — watch out!

2 ** 3 ** 2 is evaluated as 2 ** (3 ** 2) = 2 ** 9 = 512, NOT (2 ** 3) ** 2 = 64. This is the one big exception to left-to-right evaluation.

Right-to-left for **: the right-hand ** runs first.
print(2 ** 3 ** 2)
print((2 ** 3) ** 2)

#Comparisons Come After Math

All the arithmetic finishes first, then the comparison is made.
print(1 + 2 > 4 - 2)
print(3 * 2 == 10 - 4)

#Logical Operators Are Last

Comparisons run before 'and'/'or', so Python checks (x > 3) and (x < 10) as two separate True/False values, then combines them.
x = 5
print(x > 3 and x < 10)
print(x < 3 or x > 4)

#Parentheses Override Everything

You can always use parentheses `()` to control the order yourself. Anything inside parentheses is evaluated first — just like in regular math. This is the clearest, safest tool you have.

Parentheses change the result entirely.
print(2 + 3 * 4)
print((2 + 3) * 4)
Tip

When in doubt, add parentheses

Even when parentheses are not strictly needed, adding them makes your intent obvious. (2 + 3) * 4 is instantly readable. Relying on hidden precedence rules makes code harder to understand — for you and for anyone reading it later.

#A Common Beginner Trap

When mixing operators in real calculations, parentheses prevent expensive mistakes.
price = 100
discount = 10
tax_rate = 0.05

# Wrong: tax is calculated only on discount, not on (price - discount)
total = price - discount * 1 + price - discount * tax_rate
print(total)

# Right: use parentheses to group what belongs together
net = price - discount
total = net + net * tax_rate
print(total)
Quick check

What does `3 + 4 * 2 ** 2` evaluate to in Python?

Key takeaways

  • Python evaluates operators in a specific order — `**` first, then `* / // %`, then `+ -`, then comparisons, then `not`, `and`, `or`.
  • When operators share the same priority, Python works left to right (except `**`, which goes right to left).
  • Parentheses always take the highest priority and let you control the order explicitly.
  • A common surprise: `2 + 3 * 4` is `14`, not `20` — multiplication wins over addition.
  • When in doubt, add parentheses — they make your code clearer and safer.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
print(2 + 3 * 4)
print((2 + 3) * 4)
Fix the bug#2

This code is supposed to calculate the average of three scores, but it gives the wrong answer. What is wrong?

fix-bug
score1 = 80
score2 = 90
score3 = 70
average = score1 + score2 + score3 / 3
print(average)
Fill in the blank#3

Complete the expression so the result is 19. Hint: exponentiation runs first, then multiplication, then addition.

result = 3 + 4 * 2 ** 
print(result)
Reorder the lines#4

Put these lines in the right order so the code correctly prints the result of 2 raised to the power of 3 squared (which is 512, not 64).

1
print(result)
2
# 3**2 runs first (right-to-left), giving 2**9 = 512
3
result = 2 ** 3 ** 2
Your turn
Practice exercise

Fix the expression below so it correctly calculates the average of three test scores: 80, 90, and 70. Right now it gives the wrong answer — figure out why and use parentheses to fix it.

Try it live — edit the code and hit Run to execute real Python:

solution.py · editable