Operator precedence is the set of rules in Python that determines the order in which operators are evaluated in an expression that contains more than one operator. It's the programming equivalent of the "order of operations" you learned in math (like PEMDAS/BODMAS).
For example, in the expression 10 + 5 * 2, Python knows to perform the multiplication (5 * 2) before the addition, because the multiplication operator (*) has a higher precedence than the addition operator (+). The result is 20, not 30.
The Precedence Hierarchy
Python has a well-defined hierarchy for its operators. Here is a simplified list from highest precedence (evaluated first) to lowest:
1. () (Parentheses): Anything inside parentheses is always evaluated first. You can use them to override the default order.
2. ** (Exponentiation):** 2 ** 3 is evaluated before 5 * 2.
3. *, /, //, % (Multiplication, Division, Floor Division, Modulus): These all have the same level of precedence and are evaluated from left to right.
4. +, - (Addition and Subtraction): These have the same precedence and are evaluated from left to right.
5. Comparison, Identity, and Membership Operators: (==, !=, >, <, is, in, etc.)
6. not (Logical NOT)
7. and (Logical AND)
8. or (Logical OR)
Understanding this hierarchy is crucial for writing correct and predictable code. When in doubt, you can always use parentheses () to explicitly control the order of evaluation, which also makes your code easier to read.
# --- 1. Basic Arithmetic Precedence ---
# Multiplication (*) has a higher precedence than addition (+).
print("--- 1. Arithmetic Precedence ---")
result1 = 10 + 5 * 2
# Python evaluates 5 * 2 first (10), then 10 + 10.
print(f"10 + 5 * 2 = {result1}") # Expected: 20
# --- 2. Using Parentheses to Control Order ---
# Parentheses () have the highest precedence and can override the default order.
print("\n--- 2. Using Parentheses ---")
result2 = (10 + 5) * 2
# Python evaluates (10 + 5) first (15), then 15 * 2.
print(f"(10 + 5) * 2 = {result2}") # Expected: 30
# --- 3. Exponentiation Precedence ---
# Exponentiation (**) has a higher precedence than multiplication (*).
print("\n--- 3. Exponentiation Precedence ---")
result3 = 5 * 2 ** 3
# Python evaluates 2 ** 3 first (8), then 5 * 8.
print(f"5 * 2 ** 3 = {result3}") # Expected: 40
# --- 4. Mixed Arithmetic Operators (Left-to-Right Evaluation) ---
# Division (/) and Multiplication (*) have the same precedence, so they are
# evaluated from left to right.
print("\n--- 4. Mixed Arithmetic ---")
result4 = 100 / 10 * 2
# Python evaluates 100 / 10 first (10.0), then 10.0 * 2.
print(f"100 / 10 * 2 = {result4}") # Expected: 20.0
# --- 5. Logical and Comparison Operator Precedence ---
# Comparison operators (like >) have higher precedence than logical operators (like 'and').
print("\n--- 5. Logical and Comparison ---")
result5 = 5 > 3 and 10 < 20
# Python evaluates (5 > 3) -> True, then (10 < 20) -> True.
# Finally, it evaluates True and True -> True.
print(f"5 > 3 and 10 < 20 = {result5}") # Expected: True
# 'and' has a higher precedence than 'or'.
result6 = True or False and False
# Python evaluates (False and False) -> False first.
# Then it evaluates True or False -> True.
print(f"True or False and False = {result6}") # Expected: True
# Using parentheses for clarity
result7 = (True or False) and False
# Python evaluates (True or False) -> True first.
# Then it evaluates True and False -> False.
print(f"(True or False) and False = {result7}") # Expected: False