Operator Precedence in Python
Let’s start with the basics and understand the need for operator precedence in Python. As computer science enthusiasts, we often try to develop impactful products with cutting-edge technologies but seldom do we care about the absolute basics of programming and the nitty-gritty that goes into formulating the logic behind the magic that we see on our screens. Operators are building blocks for writing efficient and logically sound code. In this article, we are going to briefly discuss the concept of operator precedence in Python
Introduction and Need of Operator Precedence in Python
From our school days, right up to becoming professional developers, we encounter mathematical equations or even logical statements that could be mind-bogglingly complex. These equations might be full of numbers, variables and different kinds of operators. When we are dealing with an expression that has more than one operator, it becomes somewhat ambiguous, in which subexpression should be evaluated first. For overcoming such problems we need to set a priority to each and every kind of operator that is available in Python.
To give you a basic intuition, of what the ambiguity in expression evaluation looks like, the example below illustrates the same.
Expr = 9**2/2
Nine exponents two division two – Plain English Representation
Example One Explanation:
Now, as there are two operators, there are two ways of evaluating this expression, the first way is to carry out the evaluation of exponent and then the division.
The second way of evaluating the expression would be to go the other way round and evaluate the division and then the exponential operation. This would look something like this:
Ambiguity in Both the Expressions
If we compare both the outputs, we observe that there is an ambiguity in the results of both evaluations, even when the expression was the same in both the cases. To combat the ambiguous nature of operators and to develop a standard on which expression evaluation would take place, we have developed a technique called ‘Operator Precedence’.It defines the order in which the operators should be evaluated in an expression. An operator with higher precedence is evaluated before an operator of lower precedence. In some cases, some operators have equal precedence. In that case, the evaluation is done using the rules of the association.
Some of the operator precedence standards are also applicable in mathematics.
The priorities of the operators are described in the table below. The table follows a decreasing order of precedence.
Operator Precedence Table in Python
Unary Plus,Unary Minus,Complement(Unary NOT)
In, not in
Let’s evaluate the expression using Python
expression = 9**2/2 print(expression)
Let’s try another example using Python:
Below is the code for it.
expression = 9+5**2*((10/5)*(9%4)) print(expression)
9+5**2*((10/5)*(9%4)) -Evaluating the leftmost nested parentheses
9+5**2*(2*(9%4)) -Evaluating the next nested parentheses
9+5**2*(2*1)-Evaluating the parenthesis
9+5**2*2-Evaluating Exponential operation