🧮 Python Operator Precedence (2025 Guide) – Complete Table & Examples
🧲 Introduction – Why Operator Precedence Matters in Python
Have you ever evaluated a complex Python expression and got an unexpected result? Chances are, operator precedence is the reason. Python evaluates expressions in a specific order, just like how we follow BODMAS in mathematics.
Understanding operator precedence helps you:
- Predict outcomes of expressions
- Write clean and bug-free code
- Avoid logical errors in conditional and arithmetic statements
By the end of this article, you’ll master the complete precedence and associativity rules for Python operators with practical examples and caveats.
🔑 What is Operator Precedence in Python?
Operator precedence determines the order in which different operators in an expression are evaluated. Python has a well-defined hierarchy, so some operators bind tighter than others.
For example:
result = 10 + 5 * 2
print(result) # Output: 20, not 30
✅ Explanation:
*has higher precedence than+, so5 * 2is evaluated first →10 + 10
📊 Python Operator Precedence Table (Highest to Lowest)
| Precedence Level | Operator(s) | Description | Associativity |
|---|---|---|---|
| 1 | () | Parentheses (grouping) | N/A |
| 2 | ** | Exponentiation | Right-to-left |
| 3 | +x, -x, ~x | Unary plus, minus, bitwise NOT | Right-to-left |
| 4 | *, /, //, % | Multiplication, division, floor, mod | Left-to-right |
| 5 | +, - | Addition, subtraction | Left-to-right |
| 6 | <<, >> | Bitwise shift | Left-to-right |
| 7 | & | Bitwise AND | Left-to-right |
| 8 | ^ | Bitwise XOR | Left-to-right |
| 9 | ` | ` | Bitwise OR |
| 10 | ==, !=, >, <, >=, <=, is, is not, in, not in | Comparisons | Left-to-right |
| 11 | not | Logical NOT | Right-to-left |
| 12 | and | Logical AND | Left-to-right |
| 13 | or | Logical OR | Left-to-right |
| 14 | if – else | Conditional expressions | Right-to-left |
| 15 | lambda | Lambda expression | Right-to-left |
| 16 | =, +=, -=, etc. | Assignment operators | Right-to-left |
⚙️ Real Python Code Examples with Explanation
Example 1: Exponentiation Before Unary Minus
x = -2 ** 3
print(x) # Output: -8
✅ Explanation:
- Equivalent to:
-(2 ** 3)→-8 - Because
**binds tighter than-
Example 2: Using Parentheses to Override Precedence
x = (10 + 5) * 2
print(x) # Output: 30
✅ Explanation:
- Parentheses force
10 + 5to be evaluated before*
Example 3: Logical vs Bitwise Precedence
x = True or False and False
print(x) # Output: True
✅ Explanation:
andhas higher precedence thanor- Equivalent to:
True or (False and False)→True
💡 Tips and Best Practices
💡 Use parentheses generously in complex expressions. They make your code easier to read and maintain.
💡 Memorize just a few key precedence levels:
- Parentheses
- Exponentiation
- Multiplicative
- Additive
- Comparisons
- Logical operators (
not,and,or)
⚠️ Common Pitfalls
⚠️ Don’t assume - is evaluated before **. Always remember:
-2 ** 3 # is -8, not 64
⚠️ Be cautious with chained logical expressions like:
a or b and c
Use parentheses to clarify intent:
a or (b and c)
📘 Summary – Key Takeaways
- Python uses a fixed precedence table to evaluate expressions.
- Higher precedence operators are executed before lower ones.
- Use parentheses to override or clarify precedence.
- Be especially careful with
**,not, and conditional expressions.
✅ FAQ – Python Operator Precedence
❓What is operator precedence in Python?
Operator precedence defines the order in which Python evaluates different operators in a complex expression. Operators with higher precedence are evaluated before those with lower precedence.
❓Does Python follow BODMAS or PEMDAS?
Python follows a similar rule to BODMAS/PEMDAS for arithmetic operations, but it extends this with additional precedence rules for logical (and, or, not), bitwise (&, |, ^), and comparison operators.
❓How can I override default operator precedence in Python?
Use parentheses () to explicitly control the order of evaluation. This improves clarity and ensures your expressions behave as expected.
# Example
result = (5 + 3) * 2 # Output: 16
❓What is the precedence of not, and, and or in Python?
Python evaluates them in this order:
not(highest)andor(lowest)
True or False and False # Evaluated as: True or (False and False)
❓Why does -2 ** 3 return -8 and not 64?
Because ** (exponentiation) has higher precedence than the unary -. So it’s interpreted as:
-(2 ** 3) # → -8
Use parentheses to change the behavior:
(-2) ** 3 # → -8
❓What is associativity in Python operator precedence?
Associativity defines the direction of evaluation for operators of the same precedence level:
- Most operators (like
+,*) are left-to-right - Some (like
**, assignment=, ternaryif–else) are right-to-left
❓Is it necessary to memorize the full operator precedence table?
Not entirely. Just focus on the key levels:
- Parentheses
- Exponentiation
- Multiplication/Division
- Addition/Subtraction
- Comparison
- Logical Operators
Use parentheses when in doubt to make expressions clear.
Share Now :
