Python Operator Precedence
In this page:
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
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
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
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
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
x = 50
print(0 <= x < 100)
Chapter Quiz — Complete all 16 topics to unlock
0/16 topics done
Complete these topics first:
- Python print()
- Python input()
- Python Format Strings
- Python f-strings
- Python String Formatting
- Python Arithmetic Operators
- Python Relational Operators
- Python Logical Operators
- Python Bitwise Operators
- Python Assignment Operators
- Python Increment & Decrement
- Python Ternary Operator
- Python Operator Precedence
- Python Identity Operators
- Python Membership Operators
- Python Operators