Lesson 12: Operator precedence and associativity
When one expression holds several operators, Python applies them in a fixed order: power first, brackets above everything. These are the rules, with the three cases that catch students out.
In short
- Precedence decides which operator runs first; associativity decides the order among operators of equal rank.
- Brackets beat everything, so use them whenever the reading is not obvious.
- `` is right-associative: `2 3 2` is `2 9`, which is 512.
- `not` comes before `and`, and `and` before `or`, which is why `True or False and False` is True.
15
levels in the precedence table
Python docs
512
value of 2 ** 3 ** 2, evaluated right to left
Python docs
Two words to keep apart
Precedence is the ranking of operators: when two of them compete for the same operand, the higher-ranked one acts first. Associativity is the tie-breaker when the operators have the same rank: left to right, or right to left.
You already use both without noticing. 2 + 3 * 4 is 14 because * outranks +. 10 - 4 - 3 is 3 because subtraction groups left to right.
The table, from strongest to weakest
| Level | Operators | Associativity |
|---|---|---|
| 1 | ( ) brackets | highest |
| 2 | ** power | right to left |
| 3 | +x, -x, ~x unary | right to left |
| 4 | *, /, //, % | left to right |
| 5 | +, - | left to right |
| 6 | <<, >> | left to right |
| 7 | & | left to right |
| 8 | ^ | left to right |
| 9 | | | left to right |
| 10 | ==, !=, >, <, >=, <=, in, is | left to right |
| 11 | not | right to left |
| 12 | and | left to right |
| 13 | or | left to right |
| 14 | =, +=, and the rest | right to left |
The program
# precedence.py — the order things happen in
print(2 + 3 * 4) # 14: * before +
print((2 + 3) * 4) # 20: brackets first
print(2 ** 3 ** 2) # 512: power is right to left
print(-2 ** 2) # -4: power before unary minus
print(10 - 4 - 3) # 3: left to right
print(100 / 10 / 2) # 5.0: left to right
print(not True and False) # False
print(True or False and False)# True: and runs before or
print(5 + 2 > 6 and 3 != 4) # True: arithmetic, then comparison, then and
How it works, step by step
2 + 3 * 4: multiplication is level 4, addition level 5, so 3 * 4 happens first. Result 14.2 ** 3 ** 2: power groups right to left, so it is2 ** (3 ** 2)=2 ** 9= 512, not 64.-2 ** 2: power binds tighter than the unary minus, so it is-(2 ** 2)= -4.not True and False:notis level 11 andandis level 12, sonotapplies first, givingFalse and False, which is False.5 + 2 > 6 and 3 != 4: arithmetic first (7), then comparisons (7 > 6and3 != 4), thenand. Both sides are true.
What it prints
14 20 512 -4 3 5.0 False True True
The only rule you need in practice
If a reader needs the table to follow your line, add brackets. (2 + 3) * 4 costs two characters and saves the next person a minute. Python programmers bracket group logic a lot for exactly this reason.
Common mistakes
- Assuming power groups left to right. It does not.
- Writing
not a == bwhen you meanta != b. They happen to agree, but the second says what you mean. - Mixing
andandorwithout brackets, then reading the line as if it were arithmetic.
Practice
Try these before moving on. Each one uses only what this lesson covered.
- Predict the value of `10 + 5 * 2 ** 2` before running it, then check with brackets added.
- Work out `not 5 > 3 and 2 == 2` by hand, then print it.
- Rewrite `a + b * c - d / e` with brackets so the order is visible to a reader.
Sources
Frequently asked questions
What is operator precedence in Python?
The fixed order in which Python applies operators when an expression contains several. Power comes first, then unary operators, then multiplication and division, then addition, then comparisons, then not, and, or, and finally assignment.
Why is 2 ** 3 ** 2 equal to 512?
Because ** associates right to left, so it is read as 2 ** (3 ** 2). That inner part is 9, and 2 to the power 9 is 512.
What is associativity in Python?
The order used when two operators have the same precedence. Subtraction and division group left to right, while power and assignment group right to left.
Is True or False and False true or false?
It is True, because and is evaluated before or. The expression is True or (False and False), which is True or False.