Python Operators
Estimated reading: 4 minutes 33 views

🧮 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 +, so 5 * 2 is evaluated first → 10 + 10

📊 Python Operator Precedence Table (Highest to Lowest)

Precedence LevelOperator(s)DescriptionAssociativity
1()Parentheses (grouping)N/A
2**ExponentiationRight-to-left
3+x, -x, ~xUnary plus, minus, bitwise NOTRight-to-left
4*, /, //, %Multiplication, division, floor, modLeft-to-right
5+, -Addition, subtractionLeft-to-right
6<<, >>Bitwise shiftLeft-to-right
7&Bitwise ANDLeft-to-right
8^Bitwise XORLeft-to-right
9``Bitwise OR
10==, !=, >, <, >=, <=, is, is not, in, not inComparisonsLeft-to-right
11notLogical NOTRight-to-left
12andLogical ANDLeft-to-right
13orLogical ORLeft-to-right
14if – elseConditional expressionsRight-to-left
15lambdaLambda expressionRight-to-left
16=, +=, -=, etc.Assignment operatorsRight-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 + 5 to be evaluated before *

Example 3: Logical vs Bitwise Precedence

x = True or False and False
print(x)  # Output: True

✅ Explanation:

  • and has higher precedence than or
  • 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:

  1. Parentheses
  2. Exponentiation
  3. Multiplicative
  4. Additive
  5. Comparisons
  6. 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:

  1. not (highest)
  2. and
  3. or (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 =, ternary if–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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python Operator Precedence

Or Copy Link

CONTENTS
Scroll to Top