Updated April 1, 2023
Introduction to Operator Precedence in Python
Operator precedence is the priority in which operators are considered in python. The role of operators is when expressions are used. the expressions are a combinative use of variables, functions, values, and operators. All these elements combine to form valid expressions. and the precedence in which the operators need to be used in these expressions is determined by the operator precedence rule in python. This rule helps to evaluate the expressions and verify the order in which the operations are carried out in these expressions.
How Operator Precedence works in Python?
As mentioned before an expression is generated from a valid set of operators, functions, variables, and associated values. the presence of these in the right format will be considered by the interpreter as a valid python expression.
Ex: Variable_a = 3 + 7
This is termed to be a valid python expression. these expressions can always be formulated in a much more complex manner. the chart of precedence depicting the priority of each and every operator is represented below,
|**||raise to the power or Exponentiation|
|– ~+||Minus , complement, and plus|
|* / % //||Multiply, divide, modulo, and floor division|
|– +||subtraction and addition|
|<< >>||shift left in bitwise and shift right is bitwise.|
|&||Bitwise ‘AND’ td>|
|^ |||Bitwise exclusive `OR’ and regular `OR’|
|<= < > >=||Comparison operators|
|<> == !=||Equality operators|
|= %= /= //= -= += *= **=||Assignment operators|
|Not is||Identity operators|
|in not in||Membership operators|
|And or not||Logical operators|
Examples to Implement Operator Precedence in Python
Below are the different examples mentioned:
var_a = 20 var_b = 10 var_c = 15 var_d = 5 var_e = 0 var_e = (var_a + var_b) * var_c / var_d #( 30 * 15 ) / 5 print("Value from (var_a + var_b) * var_c / var_d is ", var_e) var_e = ((var_a + var_b) * var_c) / var_d # (30 * 15 ) / 5 print("Value from (var_a + var_b) * var_c) / var_d is ", var_e) var_e = (var_a + var_b) * (var_c / var_d); # (30) * (15/5) print("Value from (var_a + var_b) * (var_c / var_d) is ", var_e) var_e = var_a + (var_b * var_c) / var_d; # 20 + (150/5) print("Value from var_a + (var_b * var_c) / var_d is ", var_e)
Explanation: Five variables are declared and these variables are multiplied and divided based on different instances of the precedence. The variable var_e is used for recording and printing all the outcomes. In the first instance and the second instance, var_a and var_b are added and multiplied by var_c and then the outcome of the multiplied value is divided by var_e. In the third instance again var_a and var_b are added and the added value is multiplied by the outcome of the division of var_c and var_d. so both these different sets get multiplied and allocated to var_e and printed in the console. Lastly, the precedence operates in such a way that var_a remains singleton. so var_b and var_e get multiplied and the multiplied outcome is been divided by var_d. the outcome of these are been added to var_a and printed to the console.
Var = 7 if Var > 1 and Var < 10: print( " \n \n Hello World ! \n" ) else: print( " \n \n END OF PROGRAM \n \n" )
Explanation: The reserved keyword AND is used for achieving the logical AND operation in python programming. The AND keyword operates in a manner that the underneath known process will acquire consign merely whilst both the declaration specified in the AND condition is true. This program is used to substantiate whether the particular integer is deceit in the middle of a clear-cut assortment of values. The mixture necessary to be confirmed is premeditated by means of AND operator. the preliminary and end values of the range are checked as superior to and slighter than values ( Value_verified > 1 and Value_verified < 10 ). When the value established falls within this range then the console message ” Hello World ! ” is printed. In case if the above conditions do not succeed then the console statement ” END OF PROGRAM ” is printed.
print( 3 ** 2 ** 2) print((133 ** 2) ** 2) print( 1 * 3 // 3) print( 15 * (2 // 3)) print(((( (26+43) *32 )-310)//2)-4*2 )
Explanation: Several more different instances of precedence operators are reflected here, this example involves more complex implementations of precedences. here the first expression is a multiplication of three values, the next one is also a very similar instance but here the use of braces allows the first set to be operated first and then multiplied with the third one. The next instance is where the division happens first followed by multiplication of the output. the next one also operates in a very similar manner. The lastly used expression is a very complex representation or use of precedence operators in an expression. it represents the combinative use of multiplication, subtraction, division in a more nested kind of expression and the output of it is also depicted in the console.
x = 11, y = 12, z = 13 print(x = y += 12)
Explanation: This example depicts how a syntax error is populated when the precedence operators are badly misplaced in the expression sequence. here in this example, the place of the + operator in occurrence with the equal operator spoils the expression and throws a syntax error onto the console. We can notice that the syntax error representation in the console clearly points to the misplaced use of ‘+=’ in the given expression.
The optimized use of precedence operators is very key for expression structuring in any programming language and from the python perspective, the precedences are very stable prioritized and sophisticatedly used.
This is a guide to Operator Precedence in Python. Here we discuss the introduction and working of Operator Precedence in Python along with code and output. You can also go through our other related articles to learn more –