Python Operator Precedence – PEMDAS & Short Circuiting

by DataFlair Team ·
Published September 9, 2018
· Updated November 19, 2018

1. Objective

In our last Python tutorial, we discussed Python sys Module. Today, we will see Python Operator Precedence. Given an expression of multiple operators, how do you go about it? What is 2+3*4%5-1? 13 or 3? To answer such questions, you will need to know what comes first. In this Python Operator Precedence tutorial, we address this issue. Moreover, we will learn PEMDAS and short-circuiting in python. At last, we will discuss the associativity of Python Operators.
So, let’s start the Python Operator Precedence tutorial.

In here, the operators in one cell evaluate left to right and exponentiation groups right to left. The lowest Precedence in Python Operator is the least binding and the highest Precedence in Python Operator is the most. It is also true that we observe the same precedence for comparisons, membership tests, and identity tests. These also have a left-to-right chaining feature.\Have a look at Python Syntax and Semantics

3. What is Python Expression?

Before we can tell you about which operator comes first, you’d want to be introduced to expressions. An expression is a combination of values, variables, operators, and function calls. Notably, the Python interpreter can evaluate a valid expression. Why don’t we take an example?

>>> 4+3

74+3 is an expression with one operator. We can also put in more than one. The precedence rules show us the way to follow an order. The divisionn has a higher precedence than addition.You must read about Python Statements

>>> 3+3/3

4.0When we use parentheses, however, we can alter the order of execution here.

>>> (3+3)/3

2.0What we conclude here is that using parentheses, we can force the operators of lower precedence to run first. Or we can say that when two operators share an operand, the one with the higher precedence gets to go first.

4. Python Operator Precedence – PEMDAS

If you’re on this page reading about Python, you sure have heard about BODMAS somewhere in your journey so far (mathematics, school). In Python, however, we come across PEMDAS:ParenthesesExponentiationMultiplicationDivisionAdditionSubtractionLet’s revise Python IterablesA mnemonic to remember that will be “Please Excuse My Dear Aunt Susie”.Let’s take an example.

>>> ((((13+5)*2)-4)/2)-13

3.0How did that happen? Let’s work it out.13+5 gives us 1818*2 gives us 3636-4 gives us 3232/2 gives us 16.0 #Note that division gives us floats!16-13 gives us 3.0

5. Python Operator Precedence – Short Circuiting

Python always evaluates the left operand before the right- even in function arguments. For expressions with and or operations, it uses short-circuiting. This means it evaluates the second operand only until it is needed. Because of this, such statements can work reliably:

6. Associativity of Operators in Python

In that table above, many cells had more than one operator. These share precedence. So then, which to evaluate first? Associativity comes to the rescue here. Many operators have left-to-right associativity.

a. Associative Operators

Multiplication (*) and Floor Division (//)

For an example, let’s consider the operators multiplication(*) and floor division(//). Watch how the left operand evaluates first:

>>> 3*5//4

3

>>> 3*(5//4)

3While both give us the same result, they do that in different ways. Watch how:For the first example:3*5 gives us 1515//4 gives us 3For the second example:5//4 gives us 13*1 gives us 3

512This is because this is equivalent to:2**(3**2)This gives us 2**9This gives us 512

b. Non-Associative Operators

Assignment and comparison operators are not associative. What this means is that x<y<z is none of the following:(x<y)<zx<(y<z)This expression is actually equivalent to (and this evaluates left-to-right):x<y and y<zLet’s revise Python Strings
So, this was all in Python Operator Precedence. Hope you like our explanation.

7. Conclusion – Python Operator Precedence

Hence, in this Python Operator Precedence tutorial, we learned about operator precedence, PEMDAS, and short-circuiting. Also, we discussed expressions and associativity in Python Operator Precedence. What would you like to read from DataFlair? Do let us know in the comments below.See also – Python StatisticsFor reference