Page Contents
Precedence and Associativity of Operators in Python explains us how Python will evaluate an expression which contains multiple operators .
Example :
10 + 2 * 3 - 6
# This is an Expression
# In the expression there are 3 operators
# which are + , * and -
# The expression can be evaluate as
10 + (2 * 3) - 6 (result = 10)
or
(10 +2) * (3 - 6) (result = -60)
# by precedence and associativity of operators
# we will understand how the above expression
# will be evaluated by Python
Precedence of Operators in Python
The following table summarizes the operator precedence in Python, from highest precedence to lowest precedence . Operators in the same box have the same precedence
Operator | Description |
---|---|
() | Parentheses |
** | Exponent |
+x , -x , ~x | Positive, negative, bitwise NOT |
* , / , // , % | Multiplication, division, floor division, remainder |
+ , - | Addition , subtraction |
<< , >> | Left shift ,. right shift |
& | Bitwise AND |
^ | Bitwise XOR |
| | Bitwise OR |
in , not in , is , is not , < , <= , > , >= , != , == | Comparison , membership , identity operators |
not | Logical NOT |
and | Logical AND |
or | Logical OR |
Now we will see precedence of operators in python with examples –
Example 1:
# * operator has higher precedence than + or /
x = 10 + 2 * 3 - 6
# so 10 + 2 * 3 - 6 will be treated
# as 10 + (2 * 3) - 6 = 10 + 6 - 6 = 10
print(x)
10
Now we will see how python will evaluate the expression mentioned in the above code . The expression contains three operators which are +
, -
, *
. From the above table we can see that *
operator has higher precedence than +
or /
. So , 10 + 2 * 3 - 6
will be evaluated as 10 + (2 * 3) - 6
= 10 + 6 - 6
= 10
.
After that x
is printed which printed 10
at output screen .
Example 2:
# and operator has higher precedence than or
x = False or True and True
# So False or True and True will be treated
# as False or (True and True)
# = False or True = True
print(x)
True
Now we will see how python will evaluate the expression mentioned in the above code . The expression contains three operators which are and
or or
. From the above table we can see that and
operator has higher precedence than or
. So , False or True and True
will be evaluated as False or (True and True)
= False or True
= True
.
After that x
is printed which printed True
at output screen .
Associativity of Operators in Python
If any expression contains multiple operators with same precedence then associativity of operator helps us to understand how python will evaluate that expression .
In the above mentioned table associativity of all the same boxed operator is left to right except for exponent operator (**
) whose associativity is right to left .
Now we will see associativity of operators in python with examples –
Example 1:
# * and % has same precedence
# and left to right associativity
x = 12 * 5 % 6
# as * and % has left to right associativity
# so 12 * 5 % 6 will be treated as
# (12 * 5) % 6 (result = 0)
# not 12 * (5 % 6) (result = 60)
print(x)
print("(12 * 5) % 6 =",(12 * 5) % 6)
print("12 * (5 % 6) =",12 * (5 % 6))
0 (12 * 5) % 6 = 0 12 * (5 % 6) = 60
As *
and %
has same precedence and left to right associativity so in the above code 12 * 5 % 6
is treated as (12 * 5) % 6
and printed 0
in the output
Example 2:
# ** has right to left associativity
x = 5 ** 1 ** 2
# as ** has right to left associativity
# so 5 ** 1 ** 2 will be evaluated as
# 5 ** (1 ** 2) (result = 5)
# not (5 ** 1) ** 2 (result = 25)
print(x)
print("5 ** (1 ** 2) =",5 ** (1 ** 2))
print("(5 ** 1) ** 2 =",(5 ** 1) ** 2)
5 5 ** (1 ** 2) = 5 (5 ** 1) ** 2 = 25
As ** has right to left associativity so 5 ** 1 ** 2
is treated as 5 ** (1 ** 2)
and printed 5
in the output .
Non associative Operators
Some operators like assignment operators and comparison operators do not have associativity in python .
Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .