# Operator precedence in Python with Example

As the mathematical operations have precedence, Python language has also defined a certain precedence for operators.

Operator precedence in Python means the order in which the Python interpreter executes operators.

It tells the Python interpreter which operator should be evaluated first if a single statement contains more than one operator.

Therefore, it is essential to understand the order of precedence to avoid the ambiguity in the expressions.

Consider the following example code in which there are two operators, + and -. What is the result of the following expression? 23 or 40?

```exp = 3 + 5 * 4
```

If we try to calculate the result of above expression, there are two ways to do it. We could multiply 5 * 4 first and then add 3 (result: 23) or add 3 + 5 first and then multiply by 4 (result: 40).

Python fixes this dilemma by the following rules of the operator precedence:

• As the multiplication is higher precedence than addition, Python interpreter first multiplies 5 * 4, and
• then adds 3, producing a result of 23.

Sometimes operator precedence in Python does not give the result as we expect. For example, consider the following expression.

```Avg = 5 + 10 + 20 / 3
```

In this expression, we are calculating an average of three numbers. Since the division operator has a higher precedence than addition operator, Python will divide 20 by 3 before adding other numbers, producing the incorrect result.

To get the correct outcome, we need to use parentheses () because the interpreter always executes the expression inside the parentheses first before the contents outside the parentheses. Look at the following expression below:

```Avg = (5 + 10 + 20) / 3
```

In the above expression, the parentheses ensure the interpreter adds the three numbers first, and then divides the sum by 3.

## Python Operator Precedence Order

The below table lists the complete order of precedence for operators by Python language.

Operator PrecedenceOperatorDescriptionAssociativity
1( ), [ ], { }ParenthesesLeft to right
2**ExponentiationRight to left
3~, +, –Bitwise NOT (Complement), unary plus and minusLeft to right
4/, *, %, //Division, multiplication, modulus, floor divisionLeft to right
5+, –Addition and subtractionLeft to right
6<<, >>Bitwise left shift and right shift operatorsLeft to right
7&Bitwise AND operatorLeft to right
8^Bitwise XOR operatorLeft to right
9|Bitwise OR operatorLeft to right
10<=, <, >, >=Comparison operatorsLeft to right
11==, !=Equality operatorsLeft to right
12=, %=, /=, //=, -=, +=, *=, **=Assignment operatorsRight to left
13is, is notIdentity operatorsLeft to right
14in, not inMembership operatorsLeft to right
15not, or, andLogical operatorsLeft to right

From the above table, the operators in the same row have the same precedence level. The precedence decreases from top to bottom. Let us take some examples based on the operator precedence in Python that will clear more.

## Python Precedence Operator Examples

Example 1:

```print(10 / 5 * 5) # Output: 10.0
```

In the above example, division operator has the highest precedence than multiplication operator. First, the 10 / 5 is evaluated, which returns 2. Then, the outcome is multiplied by 5 i.e., (2 * 5). Thus the result is 10.0.

Example 2:

```print(10 - 10 + 10 / 10 * 10) # Output: 10.0
```

Python interpreter evaluates the above expression in the following steps.

1. Since the division operator has higher precedence than other operators like multiplication, addition, and subtraction. Therefore, the Python interpreter will first evaluate (10 / 10), which returns 1. Now, the expression is 10 – 10 + 1 * 10.

2. Since the multiplication operator has higher precedence level than other operators +, and -. Therefore, the interpreter evaluates (1 * 10), which returns 10. Now, the expression is 10 – 10 + 10.

3. The addition operator + is higher precedence than subtraction operator -. Hence, the interpreter evaluates (10 + 10), which will return 20. Now, the expression is 20 – 10.

5. The final outcome is 10.0.

Example 3:

```print(((6 - 3) + 2 * 4) * 8 / 4) # Output: 22.0
```

Python interpreter evaluates the above expression in the following steps.

1. (6 – 3) = 3
2. 2 * 4 = 8
3. 3 + 8 = 11
4. 8 /4 = 2
5. 11 * 2 = 22.0

Example 4:

```print(2 ** 3 + 4 // 7 - 6 * 9) # Output: -46
```

The above expression evaluates in the following steps:

1. Since the power operator has higher precedence than other operators like division, multiplication, addition, etc. Therefore, the Python interpreter will first evaluate (2 ** 3), which returns 8. Now, the expression is 8 + 4 // 7 – 6 * 9.

2. Since the multiplication operator has higher precedence level than other operators //, +, -, etc. Therefore, the interpreter evaluates (6 * 9), which returns 54. Now, the expression is 8 + 4 // 7 – 54.

3. As the floor division operator has higher precedence than + and – operators, therefore, the interpreter first evaluates (4 // 7), which returns 0. Now, the expression is 8 + 0 – 54.

4. The addition operator + is higher precedence than subtraction operator -. Hence, the interpreter evaluates (8 + 0), which will return 8. Now, the expression is 8 – 54.

5. The final value is -46.

Example 5:

```print(not True or True) # Output: True
```

In this example, the not operator has a higher precedence level. Therefore, the interpreter negates the True value to False. Then, the or operator combines True and False, which outcomes True value at the end. Hence, the final result is True.

Example 6:

```p = 1
q = 2
if(p > 0 and q > 0):
print('p and q are positive integer numbers.')
```
```Output:
p and q are positive integer numbers.
```

Since the comparison operators have higher precedence than logical operators, therefore, Python interpreter first evaluates the relational > operator and then evaluates logical and operator.

## Associativity in Python

Associativity in Python is the order in which the Python interpreter evaluates the expression that contains more than one operator of the same precedence.

As you can see in the above table, more than one operator is present in the same group. These operators are of the same precedence level.

For example, division and multiplication exist in the same group and have the same precedence. Hence, if both of them are present in an expression, the interpreter will evaluate it from left to right, i.e., left first.

The following example code represents how Python evaluates the expression by considering associativity.

Example 1:

```print(10 * 20 / 10)
print(20 / 10 * 10)
```
```Output:
20.0
20.0
```

In this example, the division and multiplication operators have the same precedence and exist in the same group. But the interpreter evaluates operators of the same group from left to right.

So, the interpreter first evaluates the division operator and then multiplication operator.

Example 2: Associativity of exponent operator from right to left.

```print(2 ** 2 ** 3)
print((2 ** 2) ** 3)
```
```Output:
256
64
```

1. In the first print statement, the interpreter evaluates the power operator ** from right to left. Therefore, it first evaluates (2 ** 3), which returns 8. Then, the interpreter evaluates 2 ** 8, which returns 256.

2. In the second print statement, the interpreter first evaluates the power operator of the inside the parentheses and then evaluates outside because the parentheses ( ) have higher precedence than power operator **.

So, the interpreter first evaluates (2 ** 2), which outcomes 4. Then, it evaluates 4 ** 3, which returns 64.

Thus, almost all operators have the left-to-right associativity except exponentiation and assignment operators, which are right to left associative.

In this tutorial, we have covered about the order of operator precedence in Python with example programs. Hope that you will have understood the basic points of operator precedence and associativity.