Module 4 · Operators — topic 15 of 16
Operator precedence
Precedence decides which operators are applied first when an expression has several. Learn Python’s precedence order from ** down to or, with examples and when to add parentheses.
Learning objectives
- Apply Python’s operator precedence order
- Evaluate mixed expressions step by step
- Use parentheses to make intent explicit
Definition
Precedence decides which operators are applied first when an expression has several. Learn Python’s precedence order from ** down to or, with examples and when to add parentheses.
Introduction
In 2 + 3 * 4, should Python add first or multiply first? Precedence rules decide: multiplication has higher precedence, so the answer is 14, just as in school maths. Python defines a precedence for every operator.
Why it matters
Expressions that mix arithmetic, comparison and logic are common in conditions. Knowing the order lets you read such expressions correctly, and knowing where the rules surprise people tells you where to add parentheses.
Detailed explanation
Arithmetic is evaluated before comparisons, and comparisons before logical operators. So a + b > c and d is ((a + b) > c) and d.
Watch out for -2 ** 2, which is -(2 ** 2) = -4, because ** is higher than unary minus.
not is lower than comparisons, so not a == b means not (a == b).
and is higher than or, so a or b and c means a or (b and c).
Parentheses always win. When an expression mixes more than two kinds of operator, add parentheses even if they aren’t required; readers shouldn’t need the table.
Syntax
- Parentheses force the order you want.
- They also document intent for readers.
- Redundant parentheses are harmless.
Code examples
Arithmetic order
14 20 8.0 -4
Comparison before logic
True False
More examples
Evaluating step by step
310 True True 8
Step-by-step explanation
- price * qty is 360; then 360 - 50 is 310, because * comes before -.
- Without parentheses, and binds first: (310 > 300 and False) or True, which is True.
- With parentheses the meaning changes to “large order and (member or coupon)”, also True here, but for different reasons.
- 3 + 4 * 2 ** 2 // 3: 2 ** 2 = 4, then 4 * 4 = 16, 16 // 3 = 5, then 3 + 5 = 8.
Expected output
Running “Evaluating step by step” prints exactly:
Practical applications
- Pricing rules in e-commerce mix arithmetic and logic; parentheses prevent costly misreadings.
- Scientific formulas are transcribed with parentheses to match the written maths.
- Code reviewers commonly request parentheses around mixed and/or conditions.
Best practices
- Add parentheses whenever an expression mixes operator kinds.
- Break very long expressions into named intermediate variables.
- Verify tricky expressions in the shell.
Common mistakes
Averaging without parentheses
Avoid
Prefer
/ is applied before +, so only b is halved.
Relying on and/or precedence silently
Avoid
Prefer
The code may be correct, but readers can’t tell whether the grouping was intended.
Key takeaways
- Precedence decides which operators apply first.
- Arithmetic > comparison > not > and > or.
- Parentheses override precedence and clarify intent.
Try it yourself
- 1Evaluate 18 - 6 / 3 + 2 * 3 by hand, then check in Python.
- 2Add parentheses to 5 + 10 > 12 and 4 * 2 == 8 or False so a reader can see the order without the table.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?