Skip to content

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.

Beginner 12 min read·Lesson 58 of 306 in Python Programming

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

Precedence (high to low)Operators
1( ) parentheses
2** exponentiation
3+x, -x, ~x unary
4*, /, //, %
5+, - binary
6<<, >>
7&
8^
9|
10==, !=, <, >, <=, >=, is, is not, in, not in
11not
12and
13or
14if – else (conditional expression)
15:= (walrus)

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

Python
result = (a + b) * c
ok = (total > 100) and (is_member or has_coupon)
  • Parentheses force the order you want.
  • They also document intent for readers.
  • Redundant parentheses are harmless.

Code examples

Arithmetic order

Python
print(2 + 3 * 4)
print((2 + 3) * 4)
print(10 - 4 / 2)
print(-2 ** 2)
Output
14
20
8.0
-4

Comparison before logic

Python
x = 7
print(x > 5 and x < 10)
print(not x == 7)
Output
True
False

More examples

Evaluating step by step

Python
price, qty, discount = 120, 3, 50
total = price * qty - discount
print(total)
is_member, has_coupon = False, True
print(total > 300 and is_member or has_coupon)
print(total > 300 and (is_member or has_coupon))
value = 3 + 4 * 2 ** 2 // 3
print(value)
Output
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:

TextOutput
310
True
True
8

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

average = a + b / 2

Prefer

average = (a + b) / 2

/ is applied before +, so only b is halved.

Relying on and/or precedence silently

Avoid

if is_admin or is_owner and is_active:

Prefer

if is_admin or (is_owner and is_active):

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

  1. 1
    Evaluate 18 - 6 / 3 + 2 * 3 by hand, then check in Python.
  2. 2
    Add 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?
Practise this topic