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
— step through the idea, then dive into the details below.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.
#A Surprising Example
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 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==,!=,<,>,<=,>=— comparisonsnot— logical NOTand— logical ANDor— 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
print(2 + 3 ** 2)
print(2 * 3 ** 2)#Multiplication Before Addition
print(10 - 2 * 3)
print(10 / 2 + 3)#Same-Level Operators Go Left to Right
print(20 - 5 - 3)
print(20 / 4 / 2)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.
print(2 ** 3 ** 2)
print((2 ** 3) ** 2)#Comparisons Come After Math
print(1 + 2 > 4 - 2)
print(3 * 2 == 10 - 4)#Logical Operators Are Last
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.
print(2 + 3 * 4)
print((2 + 3) * 4)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
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)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.
What does this code print?
print(2 + 3 * 4)
print((2 + 3) * 4)This code is supposed to calculate the average of three scores, but it gives the wrong answer. What is wrong?
score1 = 80
score2 = 90
score3 = 70
average = score1 + score2 + score3 / 3
print(average)Complete the expression so the result is 19. Hint: exponentiation runs first, then multiplication, then addition.
result = 3 + 4 * 2 ** print(result)
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).
print(result)
# 3**2 runs first (right-to-left), giving 2**9 = 512
result = 2 ** 3 ** 2
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: