Operator Precedence in Python
In Python, operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before those with lower precedence. Here’s a detailed list of operators, from highest to lowest precedence, along with examples to illustrate their use.
Parentheses (())
Parentheses have the highest precedence and can be used to control the order of evaluation in expressions.
result = (3 + 5) * 2 # The expression inside the parentheses is evaluated first: (3 + 5) = 8 # Then, 8 is multiplied by 2, resulting in 16.
Exponentiation (**)
Exponentiation has the next highest precedence. It is evaluated before other arithmetic operations.
result = 2 ** 3 # 2 ** 3 calculates 2 raised to the power of 3, which is 8.
Unary Operators (+, –, ~)
Unary operators apply to a single operand. The unary + indicates positive numbers, – is for negation, and ~ is for bitwise NOT.
result = -5 # The unary minus operator negates the value, so the result is -5. result = +5 # The unary plus does not change the value, so the result is 5. result = ~5 # The bitwise NOT operator inverts the bits of 5 (binary 0101), resulting in -6.
Multiplication, Division, Floor Division, Modulo (*, /, //, %)
These operators perform basic arithmetic operations and have precedence lower than exponentiation and unary operators.
Addition and Subtraction (+, –)
Addition and subtraction operators have lower precedence compared to multiplication and division.
result = 5 + 3 # Addition # 5 + 3 equals 8. result = 5 - 3 # Subtraction # 5 - 3 equals 2.
Bitwise Shift Operators (<<, >>)
Bitwise shift operators have precedence after basic arithmetic operations.
result = 5 << 1 # Left Shift # 5 in binary is 0101. Shifting left by 1 bit results in 1010, which is 10 in decimal. result = 5 >> 1 # Right Shift # 5 in binary is 0101. Shifting right by 1 bit results in 0010, which is 2 in decimal.
Bitwise AND, XOR, OR (&, ^, |)
These bitwise operators have precedence lower than shift operators.
result = 5 & 3 # Bitwise AND # 5 in binary is 0101 and 3 is 0011. The AND operation results in 0001, which is 1 in decimal. result = 5 ^ 3 # Bitwise XOR # 5 in binary is 0101 and 3 is 0011. The XOR operation results in 0110, which is 6 in decimal. result = 5 | 3 # Bitwise OR # 5 in binary is 0101 and 3 is 0011. The OR operation results in 0111, which is 7 in decimal.
Comparison Operators (==, !=, >, <, >=, <=)
Comparison operators are used to compare values and have precedence lower than bitwise operators.
result = 5 == 3 # Checks if 5 is equal to 3. The result is False. result = 5 != 3 # Checks if 5 is not equal to 3. The result is True. result = 5 > 3 # Checks if 5 is greater than 3. The result is True. result = 5 < 3 # Checks if 5 is less than 3. The result is False. result = 5 >= 3 # Checks if 5 is greater than or equal to 3. The result is True. result = 5 <= 3 # Checks if 5 is less than or equal to 3. The result is False.
Identity Operators (is, is not)
Identity operators check if two variables refer to the same object in memory and have lower precedence than comparison operators.
a = [1, 2, 3] b = a result = a is b # a and b refer to the same object, so the result is True. c = [1, 2, 3] result = a is c # a and c refer to different objects, so the result is False. result = a is not c # a and c refer to different objects, so the result is True.
Membership Operators (in, not in)
Membership operators check if a value is present in a sequence and have precedence lower than identity operators.
result = 2 in [1, 2, 3] # Checks if 2 is in the list [1, 2, 3]. The result is True. result = 4 not in [1, 2, 3] # Checks if 4 is not in the list [1, 2, 3]. The result is True.
Logical NOT (not), AND (and), OR (or)
Logical operators are used to combine boolean values and have the lowest precedence.
result = not True # The NOT operator inverts the value. The result is False. result = True and False # The AND operator returns True only if both operands are True. Here, the result is False. result = True or False # The OR operator returns True if at least one operand is True. Here, the result is True.
Conditional Expressions (if-else)
Conditional expressions, also known as ternary operators, evaluate to one of two values based on a condition.
result = "Yes" if 5 > 3 else "No" # Since 5 is greater than 3, the result is "Yes".
Assignment Operators (=, +=, -=, *=, /=, etc.)
Assignment operators are used to assign and modify values of variables and have the lowest precedence.
a = 5 a += 3 # Equivalent to a = a + 3 # Now, a is 8.
Comprehensive Example
To illustrate how operator precedence works together, consider this complex expression:
result = 5 + 2 * 3 ** 2 / (7 - 3) % 2
- Parentheses: Evaluate (7 – 3) first → 4
- Exponentiation: Evaluate 3 ** 2 → 9
- Division: Evaluate 2 * 9 / 4 → 18 / 4 → 4.5
- Modulo: Evaluate 4.5 % 2 → 0.5
- Addition: Evaluate 5 + 0.5 → 5.5
This, result will be 5.5.