Python Operators Precedence

Python Operators Precedence – Learn Python with me

In our previous chapter, we had a look at different kinds of Python Operators. However, as a programmer, you might need to know the execution order for the operators by Python if several operators are present in an expression. We will have a detailed look at them in this post. Without wasting much time, let us dive into the topic, Python Operators Precedence.


Find the link here to all chapters of the Python tutorial: Learn Python


Python Operators Precedence

As mentioned above, you should know each operator’s order in Python when you stuff in so many of them in your programs as a programmer. The set of rules that define this sequence of execution of operators is called Python Operator precedence. Listed below is the table with the sequence of operators precedence by decreasing order.

OperatorName
( ) Parentheses
**Exponentiation
-, ~Unary minus, Bitwise Complement
*, /, //, %Multiplication, Division, Floor Division, Modulus
+, –Addition, Subtraction
<<, >>Bitwise Left Shift, Bitwise Right Shift
&Bitwise AND
^Bitwise XOR
|Bitwise OR
>, >=, <, <=, ==, !=Relational (comparison) operators
=, %=, /=, //=, -=, +=, *=, **=Assignment operators
is, is notIdentity operators
in, not inMembership operators
notLogical not
orLogical or
andLogical and

Let us now look at some illustrations below.

food = "fruit"
cash = 0
if food == "fruit" or food =="meals" and cash >=2:
    print("Your food will be delivered")
else:
    print("We cannot provide you food")

Output

Your food will be delivered

In the above program, you can see that I was able to execute and get the output. However, it is not the desired output I was looking for. I declared the cash as 0, and as per the logic I came up with, the message “We cannot provide you food” should have been displayed because the logic says to deliver food only for those with cash greater than or equal to 2.

Let us now re-write the same program with parentheses, and you will see that Python will display our desired output.

food = "fruit"
cash = 0
if (food == "fruit" or food =="meals") and cash >=2:
    print("Your food will be delivered")
else:
    print("We cannot provide you food")

Output

We cannot provide you food

Python Operators Associativity

When more operators are present in the same group, there could be chances that they share the same precedence. In such cases, associativity will help you determine the sequence of operations. Almost every operator has left-to-right associativity, as shown in the example below.

# Left-right associativity
# Output: 2
print(7 * 2 // 5)

# Shows left-right associativity
# Output: 0
print(7 * (2 // 5))

Output

2
0

In the example mentioned above, we used multiplication and floor division which shares the same precedence. That’s why, when we did not use the parentheses, the left-right associativity was followed, and it produced output 2.

Important Note: Operators such as Exponentiation has the right to left associativity, as shown in the program below

# right-left associativity of **
# Output: 6561, Since 3**(2**3) = 3**8
print(3 ** 2 ** 3)

# If 3 needs to be exponated first, need to use ()
# Output: 729
print((3 ** 2) ** 3)

Output

6561
729

Non Associativity of Operators

In python, operators such as comparison and assignment do not possess the associativity properties. For example, a<b<c neither means (a<b)<c nor a<(b<c). And instead, it means a<b and b<c, which is processed from left to right.

Moreover, assignments such as a=b=c=1 is perfectly legitimate in Python and a=b=c+=2 will result in error.

# Initialize a, b, c
a = b = c = 1

# The below expression is invalid and will display error in output

a = b = c+= 2

Output

  File "E:\Python\pythonProject1\venv\OperatorPrecedence.py", line 33
    a = b = c+= 2
             ^
SyntaxError: invalid syntax

We hope you have understood the Python Operators precedence through this post. For more similar posts, stay tuned to our blog. Please do not hesitate to share your comments or queries in the comments section below.

Source

Scroll to Top