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

Python Operator Precedence

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)

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.