LambdaLabTM
Computer Science · Class 11 · Expressions & Operators
ExpressionsExam favourite⏱️ 14 min read

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:

Simply left to right
(2 + 3) * 4 → 5 * 4 → 20

If you solve it like a sentence, you get 20. Python does not do this.

Highest rank first
2 + (3 * 4) → 2 + 12 → 14

* has a higher rank than +, so it is solved first. The correct answer is 14.

Python prompt — interactive mode
>>> print(2 + 3 * 4)
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.

Precedence

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.

Associativity

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.

Key Takeaway
Precedence decides which operator is solved first when the ranks are different. Associativity decides which operator is solved first when the ranks are the same.

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.

⬆ Highest precedence — solved first
RankOperatorsAssociativity
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 laterright → left
4* / // %multiplication, division, floor division, remainderleft → right
5+ -addition, subtractionleft → right
6<< >>latershift operatorsleft → right
7&laterbitwise ANDleft → right
8^laterbitwise XORleft → right
9|laterbitwise ORleft → right
10< <= > >= == !=relational operators (comparisons)in, not in, is and is not are also on this rank — they come laterleft → right
11notlogical NOTright → left
12andlogical ANDleft → right
13orlogical ORleft → right
14if … elselaterconditional expressionright → left
15lambdalaterlambda expressionright → left
16= += -= *= /= //= %= **=laterassignment operatorsright → left
⬇ Lowest precedence — solved last
About the grey rows
The rows marked later are operators you have not learnt yet — the bitwise operators (&, |, ^, <<, >>), 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.

not

not 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:

Python prompt — interactive mode
>>> print(2 ** 3 ** 2)
512

Why is the answer 512? Because the ** on the right side is solved first:

✓ Right to left — what Python does
2 ** 3 ** 2
= 2 ** (3 ** 2)
= 2 ** 9
= 512
✗ Left to right — the wrong answer
2 ** 3 ** 2
= (2 ** 3) ** 2
= 8 ** 2
= 64
Remember this rule
When an expression has two ** 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.

Choose an expression, then solve one operator at a time
the expression
2+3**0*4
Write it like this in the exam
Write one step in each line, and solve only one operator in each step. It takes a little more time than solving it in your head, but you will not make small mistakes, and you will get full marks.

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 = 4
10 - 3 ** 4 + 144 / 12** still has the highest rank: 3 ** 4 = 81
10 - 81 + 144 / 12/ has a higher rank than + and -. And / always gives a float: 144 / 12 = 12.0
10 - 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.
Python prompt — interactive mode
>>> print(10 - 3 ** 2 ** 2 + 144 / 12)
-59.0

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:

Python prompt — interactive mode
>>> print(2 + 3 > 4 and 10 % 3 == 1)
True
12 + 3 > 4 and 10 % 3 == 1the arithmetic operators are solved first
25 > 4 and 1 == 1now the comparisons can be solved
3True and Trueand finally the logical operator
4Trueone value is left — this is the answer
Key Takeaway
An operator cannot be solved until its operands are ready. That is why the chart keeps arithmetic above the comparisons, and the comparisons above and 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.

Python prompt — interactive mode
>>> print(2 + 3 * 4) # * is solved first
14
>>> print((2 + 3) * 4) # the bracket is solved first
20
If you are not sure, use brackets
In a real program, nobody gives you extra marks for remembering the chart. If an expression is long, put brackets in it. Then your code says exactly what you mean, and nobody can read it wrongly. But in the exam you still have to know the chart.
Python prompt — interactive mode
# Try these well-known expressions. Guess the answer before you press Enter.
>>>
try

9Recap

Key Takeaway
Precedence is the ranking of operators. Out of the ones you have learnt: brackets → *** / // % + - → comparisons → notand 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.
Quick Check

What does print(2 + 3 * 4) show?

Quick Check

What does print(2 ** 3 ** 2) show?

Quick Check

What does print(3 - 2 ** 2 ** 3 + 99 / 11) show?

Quick Check

Which of these expressions is False?