Python Operator Precedence Guide

Operator precedence determines the order in which Python performs calculations when an expression contains multiple operators. If you do not understand this order, your code will produce unexpected results.

Here is an in-depth guide to how Python prioritizes operations, ordered from Highest Priority (evaluated first) to Lowest Priority (evaluated last).

1. Parentheses ( )

Precedence: Highest

Parentheses always override standard precedence rules. Python evaluates any expression inside parentheses before moving to the outside. This is your primary tool for forcing the order of operations.

Code Example: Forcing Order

# Without parentheses: Multiplication happens before Addition result_1 = 5 + 10 * 2 # 10 * 2 = 20, then 5 + 20 = 25 # With parentheses: Addition is forced first result_2 = (5 + 10) * 2 # 5 + 10 = 15, then 15 * 2 = 30 print(f"Standard: {result_1}") print(f"Forced: {result_2}")
result_1 = 5 + 10 * 2
result_2 = (5 + 10) * 2

print(f"Standard: {result_1}")
print(f"Forced:   {result_2}")
Your output will appear here...

2. Exponentiation **

Precedence: Second Highest

Exponents (powers) are calculated before multiplication, division, addition, and subtraction.

Crucial Note: Exponentiation is unique because it evaluates from Right-to-Left.
2 ** 3 ** 2 is calculated as 2 ** (3 ** 2), not (2 ** 3) ** 2.

Code Example: Right-to-Left Evaluation

# Power happens before multiplication result = 3 * 2 ** 3 # 2**3 is 8, then 3 * 8 = 24 print(result) # Right-to-Left evaluation # 3 ** 2 = 9 # 2 ** 9 = 512 power_chain = 2 ** 3 ** 2 print(power_chain)
result = 3 * 2 ** 3
print(result)

power_chain = 2 ** 3 ** 2 
print(power_chain)
Your output will appear here...

3. Unary Operators +x, -x, ~x

Precedence: High

These operators apply to a single value immediately to their right. This includes positive signs, negative signs, and bitwise NOT.

The negative sign - binds tighter than multiplication but looser than exponentiation.

  • -3 ** 2 results in -9, because it calculates 3 ** 2 first, then applies the negative.
  • (-3) ** 2 results in 9.

Code Example: Unary vs Multiplication

x = 10 # Bitwise NOT happens before multiplication result = ~x * 2 # ~10 is -11 # -11 * 2 = -22 print(result)
x = 10
result = ~x * 2  
print(result)
Your output will appear here...

4. Multiplication, Division, Modulo *, /, //, %

Precedence: Medium

These four operators share the same precedence level. When they appear together, Python evaluates them from Left-to-Right.

Code Example: Left-to-Right Evaluation

# Evaluated Left to Right result = 100 / 10 * 5 # Step 1: 100 / 10 = 10.0 # Step 2: 10.0 * 5 = 50.0 print(result)
result = 100 / 10 * 5  
print(result)
Your output will appear here...

5. Addition and Subtraction +, -

Precedence: Medium-Low

These happen after multiplication and division are complete. They also evaluate Left-to-Right.

Code Example: Math Order

# Multiplication is done first, then subtraction result = 10 - 2 * 3 # Step 1: 2 * 3 = 6 # Step 2: 10 - 6 = 4 print(result)
result = 10 - 2 * 3 
print(result)
Your output will appear here...

6. Bitwise Shifts <<, >>

Precedence: Low (but higher than bitwise logic)

Bit shifts happen after arithmetic (like addition) but before bitwise AND/OR/XOR.

Code Example: Shifts vs Arithmetic

# Addition happens before the shift result = 8 >> 1 + 1 # Step 1: 1 + 1 = 2 # Step 2: 8 >> 2 = 2 print(result)
result = 8 >> 1 + 1 
print(result)
Your output will appear here...

7. Bitwise Logical Operators &, ^, |

Precedence: Lower

These operators have their own internal hierarchy:

  1. Bitwise AND (&) is evaluated first.
  2. Bitwise XOR (^) is evaluated second.
  3. Bitwise OR (|) is evaluated last.

Code Example: Bitwise Hierarchy

# AND happens before OR result = 5 | 3 & 2 # Binary 3 (011) & 2 (010) = 2 (010) # 5 (101) | 2 (010) = 7 (111) print(result)
result = 5 | 3 & 2
print(result)
Your output will appear here...

8. Comparison Operators ==, !=, >, <, is, in

Precedence: Very Low

All math and bitwise operations are completed before Python compares the results. All comparison operators have the same precedence level.

Code Example: Math vs Comparisons

# Math is done first, then comparison result = 5 + 5 == 10 # Step 1: 5 + 5 = 10 # Step 2: 10 == 10 is True print(result)
result = 5 + 5 == 10
print(result)
Your output will appear here...

9. Boolean Logical Operators not, and, or

Precedence: Lowest

These are used to combine True/False values. They follow a specific order:

  1. not (High)
  2. and (Medium)
  3. or (Low)

Code Example: Boolean Logic Order

# 'and' binds tighter than 'or' result = True or False and False # Step 1: False and False = False # Step 2: True or False = True print(result)
result = True or False and False
print(result)
Your output will appear here...

Complete Precedence Summary Table

Rank Operator Description Associativity
1 (...) Parentheses N/A
2 ** Exponentiation Right-to-Left
3 +x, -x, ~x Unary Plus, Minus, NOT Left-to-Right
4 *, /, //, % Mult, Div, Floor, Modulo Left-to-Right
5 +, - Addition, Subtraction Left-to-Right
6 <<, >> Bitwise Shifts Left-to-Right
7 & Bitwise AND Left-to-Right
8 ^ Bitwise XOR Left-to-Right
9 | Bitwise OR Left-to-Right
10 ==, !=, >... Comparisons Left-to-Right
11 not Boolean NOT Left-to-Right
12 and Boolean AND Left-to-Right
13 or Boolean OR Left-to-Right

While memorizing this table is useful for debugging, you should not rely on it when writing code. Always use parentheses.

Writing (a + b) * c is infinitely better than writing a + b * c and hoping the next developer remembers the precedence rules. Explicit code is better than implicit code.

🏆

Lesson Completed

You have successfully learned about Python Operator Precedence, the "Order of Operations" for variables, and why Parentheses are your best friend.

📘

Full Python Course

Master Python with 11+ hours of content, 50+ exercises, and real-world projects.

Enroll Now
📝

Next Lesson

Continue with the next lesson on Python Control Flow.

Next Lesson ->
Scroll to Top