← Back to Python Course | Chapter 2: Input, Output & Operators | Lesson 13 of 16

Python Operator Precedence

Operator precedence is the order in which Python does the parts of a math problem, just like doing multiplication before addition in school. Brackets let you decide what happens first.
Syntax
python
result = a + b * c ** d  # ** first, then *, then +
result = (a + b) * c  # parentheses override

Arithmetic Precedence

Python follows the same order-of-operations rules taught in math class: multiplication and division are evaluated before addition and subtraction, so 2 + 3 * 4 evaluates to 14, not 20.

Example: Arithmetic Precedence

python
print(2 + 3 * 4)

Exponentiation Precedence

Exponentiation (**) binds tighter than any other arithmetic operator, meaning it's evaluated first even before multiplication or division — 2 * 3 ** 2 computes 3 ** 2 first, giving 2 * 9 = 18.

Example: Exponentiation Precedence

python
print(2 * 3 ** 2)

Relational and Logical Precedence

Comparison operators like > and == are evaluated before logical operators like and/or, so a > 5 and b < 10 doesn't need extra parentheses around each comparison to work as intended — Python already groups them that way.

Example: Relational and Logical Precedence

python
a, b = 6, 5
print(a > 5 and b < 10)

Left-to-Right Evaluation

When two operators share the same precedence level, Python evaluates them left to right (this is called left-associativity) — so 10 - 3 - 2 computes (10 - 3) - 2 = 5, not 10 - (3 - 2).

Example: Left-to-Right Evaluation

python
print(10 - 3 - 2)

Chained Comparisons

Chained comparisons like 0 <= x < 100 are evaluated in sequence left to right and short-circuit like and does, making range checks both concise to write and efficient to evaluate.

Example: Chained Comparisons

python
x = 50
print(0 <= x < 100)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting 2 + 3 * 4 to be 20; multiplication runs first, so it is 14 (use parentheses).
  2. Assuming -2 ** 2 is 4; ** binds tighter than unary minus, so it is -4.
  3. Reading not a == b as (not a) == b; it is not (a == b) because not has lower precedence than ==.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.