Operator precedence is a set of rules which determines the order in which multiple operations in an expression are carried out.
Every operator in python is given a precedence.
This means every operator may have
lower preference, same preference, or higher preference
over the other operator.
For example, the operator * has
higher preference over the operator +.
Consider the above expression 1 + 4 * 2.
Here, + is the first operator,
but * has higher preference over +,
So, first the term 4 * 2 is evaluated to 8;
then the result is added to the first term 1. So the final output is 9.
1 + 4 * 2 = 9 ✔
1 + 4 * 2 = 10 ❌
Most commonly used arithmetic operators like exponent, multiplication, division, addition and subtraction
have higher to lower precedences respectively. Operators inside a pair of parentheses have the highest order of
precedence out of all. The acronym PEMDAS is remembered to memorize the sequence.
Here is a table displaying the operators from highest to lowest precedence (top to bottom).
Operator | Name |
---|---|
() * | parentheses * |
** | exponent |
+x, -x, ~x | unary positive, unary negation, bitwise negation |
*, /, //, % | multiplication, division, floor division, modulo |
+, - | addition, subtraction |
<<, >> | bitwise shift left, right |
& | bitwise AND |
^ | bitwise XOR |
| | bitwise OR |
==, !=, <, <=, >, >=, is, is not, in, not in | comparison, identity and membership |
not | logical NOT |
and | logical AND |
or | logical OR |
:= | walrus |
*Truly speaking, a pair of parentheses is not an operator. It only raises the priority of the operator inside it to the highest.
Note: In the above, the ~x, <<, >>, &, | and ^ are bitwise operators which require the conception of binary number system. The use of them will be studied in a seperate article.
The operator % has higher precedence over +. What will be the final value of 3 + 14 % 5 ?
Here, 14 % 5 is evaluated to 4 first. Then 3 + 4 is evaluated to 7. It is the precedence rule that forces to evaluate 14 % 5 first, not 3 + 14.
3 + 14 % 5 = 7 ✔
3 + 14 % 5 = 2 ❌
The operator ** has higher precedence over //. What will be the final value of 1 - 29 // 2 ** 3 ?
Here, there are three operators.
** has the highest precedence,
// has higher precedence than -.
So, first 2 ** 3 is evaluated to 8.
Then 29 // 8 is evaluated to 3.
Then 1 - 3 is evaluated to -2.
When two operators have the same precedence,
the order of evaluation is decided
by their associativity, either left to right,
or right to left.
For example, the +
and - have
the same operator precedence, and left to right associativity.
So, whichever operator is in the left, will be evaluated first.
Take the example of the expression 5 - 3 + 4.
Here, both the - and + have the same precedence. Their associativity is from left to right. So, the left part 5 - 3 is evaluated first.
6 * 3 / 9
In the above expression, the * and / have the same precedence, so their associativity (left to right) is applied, and the sub-expression 6 * 3 is evaluated first; the final result is 2.0.
Most operators have left to right associativity, while the arithmetic operator ** and the logical not has right to left associativity.
Due to the right to left associativity of **, 4 ** 2 is evaluated first (=16); then 6 ** 16 is evaluated to 2821109907456.
Take the above example. If we put 6 ** 4 inside parentheses, the order of operation will change.
In this case, 6 ** 4 is evaluated first (=1296); then 1296 ** 2 is evaluated to 1679616.
In the above **
has higher precedence over -.
So, 8 ** 2 is evaluated first,
and the result is -64.
If our purpose is to find the square of -8, we should put it inside parentheses.
The above is a python script to calculate the perimeter of a rectangle. In line 4, the pair of parentheses forces the interpreter to perform the addition before multiplication. If we do not put the pair of parentheses, we will get a wrong perimeter of 21.
The logical operator not
has the highest precedence over and
and or.
The logical and
has higher precedence over
the logical or.
Let us build a logic to check this.
Suppose, to enter a ceremony, a person must be an adult,
and she should be either masked, or vaccinated.
The value of variable entry
depends uopn the variables masked, vaccinated
and adult.
A person can enter the ceremony only if value of entry is True;
a False will disallow her to enter.
First we assume that the person is both masked and vaccinated, but not adult.
Then above should be the variable assignments.
Then we prepare the logic:
We see that the person is not adult, still getting an entry!
So there is an error in the logic. The and
having higher precedence over the or,
the sub-expression vaccinated and adult
is executed first, returning a False; then True or False
returns True
(please check logical operators page).
So we change the logic in the following window:
By putting masked or vaccinated
inside parentheses,
we force the intepreter to execute the or operation first.
Now the person cannot enter the ceremony for not being adult.
To get a True output, variable adult
must be equal to True, and then one of the variables
masked or vaccinated must be
True.
--- x ---
Want to leave a message for me?