Operators Percedence and Associativity

M3-R5.1 · Chapter 4: Operators, Expressions and Python Statements · 3 min read

Operator Percedence & Associativity 

When Python sees an expression like 3 + 4 * 2, it doesn't just go left to right — it decides which operation to do first based on a set of rules called operator precedence.

Operators with higher precedence are evaluated first. 

Associativity tells Python what to do when two operators have the same precedence — whether to go left-to-right or right-to-left.

Precedence table (high → low)

#

Operator(s)

Meaning

Direction

1

() [] {}

Parentheses & braces

L→R

2

[i] [i:j]

Subscription, slicing

L→R

3

await x

Await expression

L→R

4

**

Exponentiation

R→L

5

+x -x ~x

Unary plus, minus, bitwise NOT

R→L

6

* @ / // %

Multiplication, division, modulus

L→R

7

+ -

Addition, subtraction

L→R

8

<< >>

Bitwise shift

L→R

9

& ^ |

Bitwise AND, XOR, OR

L→R

10

== != > >= < <= in not in is is not

Comparisons, identity, membership

L→R

11

not

Logical NOT

R→L

11

and or

Logical AND / OR

L→R

12

if–else

Conditional expression (ternary)

R→L

13

lambda

Lambda expression

N/A

14

:=

Walrus (assignment expression)

R→L

Python Operators Precedence Rule – PEMDAS

We might have heard about the BODMAS rule in your school’s mathematics class. Python also uses a similar type of rule known as PEMDAS.

The precedence of operators is listed from High to low.

To remember the abbreviations, we have a funny concept “Please Excuse Me Dear Aunt Sally

Now we apply the PEMDAS rule and evaluate the following expression –

Let Evaluate -   ((((6+4)*2)-10)//2)-4*2      

Some Practice Questions - 

Q1 - 3 + 5 * 2

Q2 - 10 - 4 + 2

Q3 - 2 ** 3 ** 2

Q4 - 20 // 3 % 2 + 1

Q5 - 2 ** 2*2**3

Q6 -True + 3 * 2 == 7

Q7 - not 5 > 3 and 2 < 4

Q8  - 3 + 2 ** 2 * 4 - 8 // 3

Q9 - ((3 + 2) ** 2 - 4 * 3) // (7 % 4)

Q10 - 5 + 3 > 2 * 4 or not 10 // 3 == 3