Operator precedence in Python determines the order in which operators are evaluated in an expression.
When an expression contains multiple operators, Python does not evaluate them from left to right blindly.
Instead, it follows a predefined priority order.
Operator Precedence Order :
| 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 | ` | ` |
| 10 | <, <=, >, >=, ==, != | Comparisons |
| 11 | not | Logical NOT |
| 12 | and | Logical AND |
| 13 | or | Logical OR |
| 14 | = | Assignment |
Example :
Python
result = 100 - 20 * 3 print(result) #output 40 #explanation First Multiplication , then Subtraction Python
result = 10 > 5 and 5 < 3 print(result) #output False