Operator Precedence
Operator precedence in Python defines the order in which operators are evaluated in an expression.
When an expression contains multiple operators, Python follows a fixed priority order to decide which operation is performed first.
Understanding operator precedence is essential to avoid logical bugs and unexpected results.
What Is Operator Precedence?
Operator precedence determines:
- Which operator is evaluated first
- How complex expressions are solved
- Why some expressions give unexpected output
python
print(10 + 5 * 2)
Output is
20, not 30, because * has higher precedence than +.Why Operator Precedence Matters
Without knowing precedence:
- Expressions may produce wrong results
- Conditions may behave incorrectly
- Debugging becomes difficult
python
print(5 > 3 and 3 < 10)
Operator Precedence Order (High to Low)
The table below shows Python operator precedence from highest to lowest.
| Precedence | Operator | Description |
|---|---|---|
| 1 | () | Parentheses |
| 2 | ** | Exponentiation |
| 3 | +x, -x, ~x | Unary operators |
| 4 | *, /, //, % | Multiplication, Division |
| 5 | +, - | Addition, Subtraction |
| 6 | <<, >> | Bitwise shifts |
| 7 | & | Bitwise AND |
| 8 | ^ | Bitwise XOR |
| 9 | | | Bitwise OR |
| 10 | <, <=, >, >=, ==, != | Comparisons |
| 11 | not | Logical NOT |
| 12 | and | Logical AND |
| 13 | or | Logical OR |
| 14 | = | Assignment |
Parentheses (())
Parentheses have the highest precedence and are used to control evaluation order.
python
print((10 + 5) * 2)
python
print(10 + (5 * 2))
Exponentiation (**)
Exponentiation is evaluated before multiplication and addition.
python
print(2 ** 3 * 2)
python
print(2 ** (3 * 2))
Arithmetic Precedence
python
print(10 + 5 * 2)
python
print(100 / 10 * 2)
python
print(100 // 10 + 3)
Unary Operators
Unary operators apply to a single operand.
python
x = -5
print(-x)
python
print(~5)
Comparison Precedence
Comparisons are evaluated after arithmetic operations.
python
print(10 + 5 > 12)
python
print(10 * 2 == 20)
Logical Operator Precedence
Logical operators have lower precedence than comparisons.
python
print(10 > 5 and 5 > 2)
python
print(10 > 5 or 5 > 20)
python
print(not 10 > 5)
Chained Comparison Precedence
Chained comparisons are evaluated left to right.
python
print(5 < 10 < 20)
python
print(5 < 10 > 3)
Bitwise Operator Precedence
python
print(5 & 3 + 1)
python
print((5 & 3) + 1)
python
print(5 & (3 + 1))
Assignment Precedence (Lowest)
Assignment has the lowest precedence.
python
x = 10 + 5 * 2
print(x)
Common Mistakes
Assuming Left-to-Right Always
python
print(10 - 5 - 2)
This is evaluated as:
python
(10 - 5) - 2
Forgetting Parentheses
python
print(5 > 3 and 3 < 1)
Best Practice
Always use parentheses when:
- Expression is complex
- Readability matters more than brevity
- Logical conditions are combined
python
if (age > 18) and (salary > 30000):
print("Eligible")
Summary
- Operator precedence controls evaluation order
- Parentheses have the highest priority
- Arithmetic operators are evaluated before comparisons
- Logical operators are evaluated last
- Assignment has the lowest precedence
- Parentheses improve clarity and correctness
Practice
- Predict output of mixed expressions
- Rewrite expressions using parentheses
- Identify precedence errors in conditions
- Convert complex expressions into readable form