Operator Precedence
One expression can have many operators. So which operator does Python solve first? It does not simply start from the left. It starts with the operator that has the highest rank. This ranking of operators is called precedence.
114 or 20?
Look at 2 + 3 * 4. It can be solved in two different ways, and the two ways give two different answers:
(2 + 3) * 4 → 5 * 4 → 20If you solve it like a sentence, you get 20. Python does not do this.
2 + (3 * 4) → 2 + 12 → 14* has a higher rank than +, so it is solved first. The correct answer is 14.
You already know this rule from maths — multiply first, then add. Python follows the same idea. But Python writes down this ranking for every operator, and not only for these two. That written ranking is called the precedence chart.
2Precedence and associativity
There are two important words in this lesson. They answer two different questions.
Which operator is solved first?
It is a ranking of operators. The operator with the higher rank is solved first, wherever it is written in the expression.
And if two operators have the same rank?
Then associativity tells you the direction in which to solve them — from left to right, or from right to left.
3The precedence chart
You already use a rule like this in maths. It is called BODMAS — brackets first, then orders (powers), then division and multiplication, and addition and subtraction at the end. Python has many more operators than maths does, so its rule is a longer list. But the idea is exactly the same as BODMAS.
This is the complete chart for Python. The operator with the highest rank is at the top, and the one with the lowest rank is at the bottom.
| Rank | Operators | Associativity |
|---|---|---|
| 1 | ( )brackets (parentheses) | left → right |
| 2 | **exponentiation (power) | right → left |
| 3 | +x -x ~xpositive sign, negative sign, bitwise NOTeach of these works on a single operand · ~ comes later | right → left |
| 4 | * / // %multiplication, division, floor division, remainder | left → right |
| 5 | + -addition, subtraction | left → right |
| 6 | << >>latershift operators | left → right |
| 7 | &laterbitwise AND | left → right |
| 8 | ^laterbitwise XOR | left → right |
| 9 | |laterbitwise OR | left → right |
| 10 | < <= > >= == !=relational operators (comparisons)in, not in, is and is not are also on this rank — they come later | left → right |
| 11 | notlogical NOT | right → left |
| 12 | andlogical AND | left → right |
| 13 | orlogical OR | left → right |
| 14 | if … elselaterconditional expression | right → left |
| 15 | lambdalaterlambda expression | right → left |
| 16 | = += -= *= /= //= %= **=laterassignment operators | right → left |
&, |, ^, <<, >>), the if … else expression, lambda, and the assignment operators. You will study each of them in due course. For now, simply see where they sit in the chart, and use the rows that are not grey.Read the chart from top to bottom. Brackets are solved first. Then the power operator. Then multiplication, division, floor division and remainder. Then addition and subtraction. So all the arithmetic is finished first. Only after that are the comparisons solved, and the logical operators are solved at the very end. This order is sensible, because and cannot join two answers before those answers are ready.
4The operators that go right to left
Look at the associativity column once again. Most rows say left to right, which is the normal direction. A few rows say right to left, and among the operators you have learnt, these are the ones you must remember:
**When an expression has two ** operators, the one on the right is solved first. This is explained below.
notnot has only one operand, and that operand is on its right. So in not not True, the right not is solved first. The sign operators +x and -x work in the same way.
Now look at ** closely, because this is the rule that is asked most often in exams:
Why is the answer 512? Because the ** on the right side is solved first:
2 ** 3 ** 2
= 2 ** (3 ** 2)
= 2 ** 9
= 5122 ** 3 ** 2
= (2 ** 3) ** 2
= 8 ** 2
= 64** operators, the one on the right is solved first. This rule is asked in exams very often, because many students get it wrong.5Solving a long expression
The method is always the same. Find the operator with the highest rank in the expression. Solve only that one operator. Write the shorter expression below it. Then do the same again, until only one value is left. If two operators have the same rank, associativity tells you which one to solve first.
Solve each expression below, one step at a time. Every step tells you which operator was chosen, and why it was chosen.
6A solved example
Here is 10 - 3 ** 2 ** 2 + 144 / 12, solved the way you should write it in your notebook:
10 - 3 ** 2 ** 2 + 144 / 12Both ** have the same rank, so we go from right to left. Solve the RIGHT one first: 2 ** 2 = 410 - 3 ** 4 + 144 / 12** still has the highest rank: 3 ** 4 = 8110 - 81 + 144 / 12/ has a higher rank than + and -. And / always gives a float: 144 / 12 = 12.010 - 81 + 12.0- and + have the same rank, so we go from left to right. Solve the LEFT one first: 10 - 81 = -71-71 + 12.0Only one operator is left. One operand is a float, so the answer is a float.-59.0This is the final answer.7All three types in one expression
First the arithmetic operators, then the relational operators, and last the logical operators. This is the order given by the chart, and it is the only order that can work:
2 + 3 > 4 and 10 % 3 == 1the arithmetic operators are solved first5 > 4 and 1 == 1now the comparisons can be solvedTrue and Trueand finally the logical operatorTrueone value is left — this is the answerand and or.8Brackets have the highest rank
Brackets are at the top of the chart. So whatever is inside the brackets is always solved first, whatever the rank of the operators inside them may be.
9Recap
** → * / // % → + - → comparisons → not → and → or. Associativity is used when two operators have the same rank. It is left to right for all of them, except ** and not, which go right to left.What does print(2 + 3 * 4) show?
What does print(2 ** 3 ** 2) show?
What does print(3 - 2 ** 2 ** 3 + 99 / 11) show?
Which of these expressions is False?