Precedence & Associativity of Operators
Precedence & Associativity of Operators
In Python, operators have different levels of precedence, which determine the order in which they are evaluated in an expression. When multiple operators appear in an expression, Python follows a set of rules to determine which operations are performed first. Additionally, associativity determines the order in which operators of the same precedence are evaluated (left-to-right or right-to-left). Here's an overview of operator precedence and associativity in Python:
Operator Precedence:
1. Parentheses `()`: Highest precedence. Operations within parentheses are evaluated first.
2. Exponentiation `**`: Next highest precedence. Exponentiation operations are evaluated from right to left.
3. Multiplication `*`, Division `/`, Floor Division `//`, Modulus `%`: These arithmetic operations have the same precedence and are evaluated from left to right.
4. Addition `+` and Subtraction `-`: Addition and subtraction have the same precedence and are evaluated from left to right.
5. Bitwise Shifts `<<` and `>>`: Bitwise left and right shifts have the same precedence and are evaluated from left to right.
6. Bitwise AND `&`: Bitwise AND has lower precedence than shifts and is evaluated from left to right.
7. Bitwise XOR `^`: Bitwise XOR has lower precedence than bitwise AND and is evaluated from left to right.
8. Bitwise OR `|`: Bitwise OR has lower precedence than bitwise XOR and is evaluated from left to right.
9. Comparison Operators (`<`, `<=`, `>`, `>=`, `==`, `!=`): These operators have the same precedence and are evaluated from left to right.
10. Boolean NOT `not`: Has lower precedence than comparison operators.
11. Boolean AND `and`: Has lower precedence than NOT.
12. Boolean OR `or`: Lowest precedence. It has the lowest priority and is evaluated last.
Associativity:
- Most operators in Python are left-associative, meaning they are evaluated from left to right when they have the same precedence. For example, `a + b + c` is evaluated as `(a + b) + c`.
- Exponentiation (`**`) is right-associative, meaning it is evaluated from right to left. For example, `a b c` is evaluated as `a (b c)`.
- Assignment operators (`=`, `+=`, `-=`, `*=`, etc.) are right-associative, and they evaluate from right to left. For example, `a = b = c` is evaluated as `a = (b = c)`.
Here's an example demonstrating operator precedence and associativity:
```python
result = 2 + 3 * 4 ** 2 / 2 - 1
```
In this expression, the order of evaluation is as follows:
1. Exponentiation: `4 ** 2` is calculated first, resulting in 16.
2. Multiplication and Division: `3 * 16 / 2` is calculated next, resulting in 24.0 (float division).
3. Addition and Subtraction: `2 + 24.0 - 1` is evaluated, resulting in the final value of `25.0`.
Understanding operator precedence and associativity is crucial for writing correct and efficient Python expressions and for avoiding unexpected results. You can use parentheses to override the default precedence and explicitly specify the order of evaluation when needed.
Comments
Post a Comment