Guide Python Intermediate

7.9 Operator Precedence and Expression Evaluation

The order Python applies operators in a mixed expression, associativity for same-precedence operators, why parentheses always win, and how nested expressions resolve from the inside out.

3 min read

Operator Precedence

What Is It?

The order in which operators are applied when an expression mixes several of them, without any parentheses to force a specific order.

Why Does It Matter?

2 + 3 * 4 is 14, not 20, because * binds tighter than + — getting this wrong silently produces a wrong (but valid-looking) result.

Precedence Table

Highest precedence at top — evaluated first:

PrecedenceOperatorsCategory
Highest()Parentheses — grouping
**Exponentiation
~x, +x, -xUnary invert/plus/minus
* / // %Multiplication, division, modulus
+ -Addition, subtraction
<< >>Bitwise shifts
&Bitwise AND
^ |Bitwise XOR, OR
== != < > <= >=Comparisons
notLogical NOT
andLogical AND
LowestorLogical OR

The practical takeaway: arithmetic > comparisons > logical, so mixed expressions read naturally without needing parentheses everywhere — but add them anyway when in doubt (see 7.17 Best Practices).

Associativity

What Is It?

When operators of the same precedence appear together, associativity decides the grouping direction. Most operators are left-associative (evaluated left to right); ** is right-associative.

>>> 10 - 2 - 3       # left-associative: (10 - 2) - 3
5
>>> 2 ** 3 ** 2       # right-associative: 2 ** (3 ** 2)
512

Parentheses

Explicit parentheses always override default precedence and associativity — the clearest way to guarantee a specific evaluation order, and often better for readability even when not strictly required.

>>> 2 + 3 * 4       # * happens first by default
14
>>> (2 + 3) * 4     # parentheses force + first
20

Expression Evaluation

Simple Expressions

A single operator applied to its operands, producing one value.

>>> 5 + 3
8

Complex Expressions

Multiple operators combined — Python resolves them via precedence, then evaluates left to right within each precedence level, respecting associativity.

>>> x = 5
>>> result = (x + 3) * 2 - 1
>>> result
15

Nested Expressions

Expressions can contain other expressions as operands, to any depth — Python resolves the innermost parentheses first, exactly like standard mathematical notation.

>>> ((2 + 3) * (4 - 1)) ** 2
225

Quick Interview Answer

“Python resolves a mixed expression by precedence first — roughly arithmetic, then bitwise, then comparisons, then logical, from highest to lowest — then by associativity when operators share the same precedence: most are left-associative, but ** is right-associative, so 2 ** 3 ** 2 is 2 ** (3 ** 2) = 512. Parentheses always override both rules and are the clearest way to make evaluation order explicit, which matters most in mixed comparison/logical expressions where the default grouping is easy to misread.”

Common Mistakes

  • Assuming and/or bind tighter than comparisons — comparisons are actually evaluated first, so 2 + 3 == 5 and 1 < 2 groups as ((2+3) == 5) and (1 < 2).
  • Forgetting ** is right-associative, misreading 2 ** 3 ** 2 as (2 ** 3) ** 2 (64) instead of the correct 2 ** (3 ** 2) (512).
  • Nesting nested parentheses so deeply that the expression becomes harder to read than the precedence issue it was meant to avoid.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form