When dealing with Operators in Python, the concept of Python operator precedence and associativity is a must to know. Otherwise, you will see unexpected outputs in your Python console.
As we all know, an expression is a combination of operands, operators and constants (values). Now, consider this simple expression,
print (7 + 10)
Since there is just one operation to be performed, its output is straightforward. But, such calculations can get complex when 2 or more operators are involved in an expression. Let's consider another expression,
print (9 + 2 - 6 * 4)
GUESS THE OUTPUT NOW?
The output is -13. In the above expression, there are 3 operators (+, -, *) involved. To avoid confusion, the compiler evaluates the expression using Python operator precedence. According to this precedence rule, multiplication has higher precedence, followed by addition and subtraction. So hence the evaluation happens this way,
Below is the table containing the precedence of all operators in Python.The operators are listed down in the decreasing order of
Operator Name () Parenthesis ** Exponent ~ Bitwise NOT *, /, %, // Multiplication, Division, Modulo, Floor Division +, – Addition, Subtraction >>, << Bitwise right and left shift & Bitwise AND ^ Bitwise XOR | Bitwise OR ==, !=, >, <, >=, <= Comparison =, +=, -=, *=, /=, %=, **=, //= Assignment is, is not Identity In, not in Membership and, or, not Logical
After having a quick look at the above table, you must have had this doubt.
"In our previous example, addition was performed before subtraction. But then, the precedence of
- is the same.Then how did we determine that addition has higher precedence than subtraction?"
That's where the associativity of operators comes into the picture.
Associativity is the order in which an expression with multiple operators of the same precedence is evaluated. Associativity can be either from left to right or right to left. Almost all the operators have left-to-right associativity, except a few.
For example, consider an expression having operators with the same precedence,
Here the multiplication
* and division
/ operators have left to right associativity. This means, a and bare multiplied first and the result thus obtained is then divided by b. This is simply because when we read the expression from left to right, multiplication comes first and hence it gets evaluated first.
Below is the table containing the associativity of all operators in Python.
Operator Name Associativity () Parenthesis Left to right ** Exponent Right to left ~ Bitwise NOT Left to right *, /, %, // Multiplication, Division, Modulo, Floor Division Left to right +, – Addition, Subtraction Left to right >>, << Bitwise right and left shift Left to right & Bitwise AND Left to right ^ Bitwise XOR Left to right | Bitwise OR Left to right ==, !=, >, <, >=, <= Comparison Left to right =, +=, -=, *=, /=, %=, **=, //= Assignment Right to left is, is not Identity Left to right In, not in Membership Left to right and, or, not Logical Left to right
In the above-given table, only exponent and assignment operators have the right to left associativity. All the other operators follow left to right associativity.
num1, num2, num3 = 2, 3, 4 print ((num1 + num2) * num3)
Explanation:Since parentheses have the highest precedence, values 2 and 3 are added first and then the resultant value 5 is multiplied with 4. Hence, the output is 20.
num1, num2, num3 = 2, 3, 4 print (num1 ** num2 + num3)
Explanation:Among the given operators, the exponent has higher precedence, and hence value 2 is raised to the power of 3 and then the resultant value 8 is to 4. Hence, the output is 12.
num1, num2, num3 = 2, 3, 4 print (~num1 + num2)
Explanation:The output of ~num1 is -3 and when added to 3, the final output is 0.
num1, num2, num3, num4 = 8, 2, 3, 6 num5 = ((num1 * num2) - (num3 + num4)) print(num5)
Explanation:Since parentheses has left to right associativity, the operands num1 and num2 get multiplied first and then the operands num3 and num4 get added. At last, the results of these get subtracted.
num1, num2, num3 = 2, 3, 2 num4 = num1 ** num2 ** num3 print(num4)
Explanation:Since the exponent operator has right to left associativity, first the operand num2 gets raised to the power of num3 and then num1 gets raised to the power of the resultant value.
num1 = 5 num1 = num2 = num3 print(num2)
Traceback (most recent call last):
File “/home/5eb00addafd751d876f2507eb42d8346.py”, line 2, in <module>
num1 = num2 = num3
NameError: name ‘num3’ is not defined
Explanation: Here, since the assignment operator has right to left associativity, the value in num3 gets assigned to num2 and num1. But, num3 is not defined. Hence the compiler throws an error.
But the below code executes without any error.
num1 = 5 num3 = num2 = num1 print(num2) print(num3)