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.

Example One:

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.

(9**2)/2

81/2

40.5

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:

9**(2/2)

9**1

9

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

 

OPERATOR

DESCRIPTION

()

Parenthesis

function_name()

Function Call

{expr},{key:value},(expr),[expr]

Set,Dictionary,Tuple,List Representation

list_name[start_index:end_index]

Slicing

list_name[index]

Subscripting

object_name.attribute_name

Attribute Reference

**

Exponent

+a,-a,~a

Unary Plus,Unary Minus,Complement(Unary NOT)

*,/,//,%

Multiplication,Division,Floor Division,Modulus

+,-

Addition,Subtraction

<<,>>

Bitwise Shift

&

Bitwise AND

^

Bitwise XOR

|

Bitwise OR

>,<,>=,<=,==,!=

Comparison

=,+=,-=,/=,%=,**=

Assignment

Is,is not

Identity

In, not in 

Membership

not

Logical NOT

and

Logical AND

or

Logical OR

lambda

Lambda Expression

 

 Python Implementation:

Example One:

Let’s evaluate the expression using Python

expression = 9**2/2
print(expression)
>> 40.5

Let’s try another example using Python:

Example Two:

Below is the code for it.

expression = 9+5**2*((10/5)*(9%4))
print(expression)
>> 59.0

Explanation:

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

9+25*2-Multiplication

9+50-Addition

59-Final Result

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *