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
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
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.
- binds tighter than multiplication but looser than exponentiation.
-3 ** 2results in-9, because it calculates3 ** 2first, then applies the negative.(-3) ** 2results in9.
Code Example: Unary vs Multiplication
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
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
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
result = 8 >> 1 + 1
print(result)
Your output will appear here...
7. Bitwise Logical Operators &, ^, |
Precedence: Lower
These operators have their own internal hierarchy:
- Bitwise AND (
&) is evaluated first. - Bitwise XOR (
^) is evaluated second. - Bitwise OR (
|) is evaluated last.
Code Example: Bitwise Hierarchy
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
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:
not(High)and(Medium)or(Low)
Code Example: Boolean Logic Order
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