Python Tutorial for Beginners #3: Operator Precedence and Associativity - Understanding the Order of Operations in Python

Raj PatelRaj Patel
10 min read

In our previous tutorial, we explored Python's essential operators - the building blocks that let us perform calculations, comparisons, and logical operations. At last, I left you all with an interesting challenge,

“5 + 3 * 2 > 10 and True or False”. Did you try to figure out what this expression evaluates to? If you guessed True, you are absolutely right. But if you are wondering why it’s True and why not something else, you’ve come to the right place.

The answer lies in understanding the two fundamental concepts that every Python Programmer must master: operator precedence and associativity. These are the invisible rules that determine the order in which Python evaluates your expression. Let us try to decode that mysterious expression step by step and discover the logic behind Python’s decision making process.

Decoding the Mystery: Step-by-Step Breakdown

5 + 3 * 2 > 10 and True or False

To solve this mystery, we need to understand that Python follows a specific order of operations. Let’s break it down step by step:

Step 1: Multiplication First

5 + (3 * 2) > 10 and True or False
5 + 6 > 10 and True or False

Step 2: Addition

(5 + 6) > 10 and True or False
11 > 10 and True or False

Step 3: Comparison

(11 > 10) and True or False
True and True or False

Step 4: Logical AND

(True and True) or False
True or False

Step 5: Logical OR

True or False
True

And that’s our answer True.

But wait - how did Python know to follow this exact sequence? The answer lies in understanding operator precedence and associativity rules.

Understanding Operator Precedence

How does Python know to multiply before adding, or handle “and” before “or“ ? The answer is operator precedence - Python’s built in ranking system for operators. Think of operator precedence like a VIP list at a club. Some operators get priority access and are processed first, while others have to wait for their turn. Python follows this hierarchy ensuring your expressions are evaluated consistently every time. Here’s Python complete operator precedence hierarchy ( from highest priority to lowest priority ):

  1. Parentheses ( ) - Always first

  2. Exponentiation **

  3. Unary operators +x, -x, ~x ( positive, negative, bitwise NOT )

  4. Multiplication, Division, Floor Division, Modulus *, /, //, %

  5. Addition, Subtraction +,-

  6. Bitwise shifts «,» ( left shift, right shift )

  7. Bitwise AND &

  8. Bitwise XOR ^

  9. Bitwise OR |

  10. Comparison Operators <, <=, >, >=,==, !=, is, is not, in, not in

  11. Logical NOT not

  12. Logical AND and

  13. Logical OR or

  14. Conditional Expressions x if condition else y

  15. Assignment Operators =, +=, -=. *=, /

Don’t worry if some of these operators feel unfamiliar - as a beginner, you’ll primarily work with arithmetic, comparison and logical operators. The bitwise operators (&, |, ^, «, ») are more advanced and used for specific programming scenarios. The key takeaway is that Python has a clear hierarchy, and understanding the most common ones will solve most of your expression evaluation puzzles. Let us look at some brain-teasing examples. Try to predict the output yourself before looking at the answers:

Example- 1

result = 2 + 3 * 4 ** 2
print(result)

What do you think result will be?

Example- 2

value = 10 > 5 and 3 < 7 or False
print(value)

Example- 3

answer = 15 / 3 + 2 * 4 - 1
print(answer)

Example- 4

mystery = not True or False and True
print(mystery)

Example- 5

puzzle = 8 % 3 + 2 ** 3 / 4
print(puzzle)

Answers and Explanations:

Example- 1 result = 2 + 3 * 4 ** 2

  1. Step 1: 4**2 = 16 (exponentiation first)

  2. Step 2: 3×16 = 48 (multiplication)

  3. Step 3: 2 + 48 = 50 (addition)

Answer: 50

Example- 2 value = 10 > 5 and 3 < 7 or False

  1. Step 1: 10 > 5= True (Comparison Operator)

  2. Step 2: 3 < 7 = True (Comparison Operator first)

  3. Step 3: True and True = True (Logical and)

  4. Step 4: True or False = True( Logical or)

Answer: True

Example- 3 answer = 15 / 3 + 2 * 4 - 1

  1. Step 1: 15 / 3= 5.0 (Division)

  2. Step 2: 2 × 4= 8 (Multiplication)

  3. Step 3: 5.0 + 8 = 13.0(Addition)

  4. Step 4: 13.0 - 1 = 12.0 (Subtraction)

Answer: 12.0

Example- 4 mystery = not True or False and True

  1. Step 1: not True = False (Logical not)

  2. Step 2: False and True = False (Logical and)

  3. Step 3: False or False = False (Logical or)

Answer: False

Example- 5 puzzle = 8 % 3 + 2 ** 3 / 4

  1. Step 1: 2**3 = 8 (Exponentiation First)

  2. Step 2: 8 % 3 = 2 (Modulus)

  3. Step 3: 8 / 4 = 2.0 (Division)

  4. Step 4: 2 + 2.0 = 4.0 (Addition)

Answer: 4

Let’s look at another example:

result = 20 - 10 - 5

What will be the answer here: 15 or 5? If both answers seem logical to you, you are about to discover something important about how Python thinks. If we move from left to right:

result = 20 - 10 - 5
#Step 1: (20 - 10) - 5
#Step 2: 10 - 5 
#Step 3: 5

But if we move from right to left:

result = 20 - 10 - 5
#Step 1: 20 - (10 - 5)
#Step 2: 20 - 5
#Step 3: 15

Now you are in dilemma which one is correct 5 or 15? This is a perfect example of associativity in action. When operators are equal in precedence, the direction of evaluation matters. This is known as associativity.

All the operators are left associative (meaning they go left to right) except for exponentiation and assignment operators(which go right to left).

Let us look at some brain-teasing examples. Try to predict the output yourself before looking at the answers:

Example- 1

result = 100 / 5 / 4

Example- 2

result = 2 ** 3 ** 2

Example- 3

a = b = c = 10

Example- 4

True and False and True

Example- 5

a < b < c

Answers and Explanations:

Example- 1 result = 100 / 5 / 4

  1. Step 1: 100 / 5 = 20.0 (Division is Left Associative)

  2. Step 2: 20.0 / 4 = 5.0 (Division)

Answer: 5

Example- 2 result = 2 ** 3 ** 2

  1. Step 1: 3 ** 2 = 9 (Exponentiation is Right Associative)

  2. Step 2: 2**9 = 512 (Exponentiation)

Answer: 512

Example- 3 a = b = c = 10

  1. Step 1: c = 10(Assignment is Right Associative)

  2. Step 2: b = c = 10 (Assignment)

  3. Step 3: a = b = c = 10 (Assignment)

Example- 4 True and False and True

  1. Step 1: True and False = False(Logical and is Left Associative)

  2. Step 2: False and True = False (Logical and)

Answer: False

Example- 5 a < b < c

This is known as Chain Comparison

  1. Step 1: a<b (Comparison operator is Left Associative)

  2. Step 2: b< c (Comparison operator)

  3. Step 3: Logical and is performed on result of step1 and step2 (step1 and step 2) or in other words a<b and b<c.

Let us look at some more brain-teasing examples for your practice involving both associativity and precedence. Try to predict the output yourself before looking at the answers:

Example- 1

result = 10 + 6 / 2 - 1

Example- 2

result = 2 + 3 ** 2 ** 2

Example- 3

result = 24 / 4 * 3 / 2

Example- 4

result = 5 > 3 and 10 / 2 == 5 or False

Example- 5

result = 15 - 3 * 2 + 8 / 4 - 1

Answers and Explanations:

Example- 1 result = 10 + 6 / 2 - 1

  1. Step 1: 6 / 2 = 3.0 (Division has highest precedence in this case)

  2. Step 2: 10.0 + 3 = 13.0 (Addition and Subtraction have same precedence but they are left associative)

  3. Step 3: 13.0 - 1 = 12.0

Answer: 12.0

Example-2 result = 2 + 3 ** 2 ** 2

  1. Step 1: 2**2 = 4(Exponentiation has highest precedence in this case and it is right associative)

  2. Step 2: 3**4 = 81 (Exponentiation is right associative)

  3. Step 3: 2 + 81 = 83 ( Addition has least precedence in this case)

Answer: 83

Example- 3 result = 24 / 4 * 3 / 2

  1. Step 1: 24 / 4 = 6.0 (Division and Multiplication have equal precedence in this case and it is left associative)

  2. Step 2: 6.0×3 = 18.0 (Multiplication and Division is left associative)

  3. Step 3: 18.0 / 2 = 9.0

Answer: 9.0

Example- 4 result = 5 > 3 and 10 / 2 == 5 or False

  1. Step 1: 10/2 = 5.0(Division has highest precedence in this case and it is left associative)

  2. Step 2: 5.0>3 = True (Comparison operator have second highest precedence in this case and they are left associative)

  3. Step 3: 5.0 == 5 ( Comparison operator have second highest precedence in this case and they are left associative)

  4. Step 4: True and True = True (Logical and operator has the next highest precedence in this case and it is left associative)

  5. Step 5: True or False = True(Logical or operator has the next highest precedence in this case and it is left associative)

Answer: True

Example- 5 result = 15 - 3 * 2 + 8 / 4 - 1

  1. Step 1: 3×2 = 6 (Division and multiplication have highest precedence in this case and it is left associative)

  2. Step 2: 8 / 4 = 2.0 (Division and multiplication have highest precedence in this case and it is left associative)

  3. Step 3: 15 - 6 = 9 (Addition and Subtraction have second highest precedence in this case and they are left associative)

  4. Step 4: 9 + 2.0 = 11.0 (Addition and Subtraction have second highest precedence in this case and they are left associative)

  5. Step 5: 11.0 - 1 = 12.0 (Addition and Subtraction have second highest precedence in this case and they are left associative)

Answer: 12.0


And there you have it! We've successfully decoded that mysterious expression from the beginning and uncovered the secret rules that Python follows. Let's recap the key takeaways:

  • Operator Precedence determines which operators get evaluated first, think of it as Python's VIP list.

  • Associativity breaks the tie when operators have equal precedence. Most of the operators have left-to-right associativity, except for exponentiation and assignment operators.

Understanding these rules helps you predict exactly how Python will evaluate any expression. Mastering operator precedence and associativity might seem overwhelming at first, but here's the good news “ you don't need to memorize every rule!” Here are some practical tips:

  • When in doubt, use parentheses to make your intentions clear. Remember, even experienced programmers sometimes use parentheses for clarity because readable code is always better than clever code.

  • Focus on the common operators first (arithmetic, comparison, logical) because most of the times you will be dealing with this operators only.

  • Practice with simple expressions before tackling complex ones

The more you practice operator precedence and associativity, the more natural it becomes. This isn't just true for operators, it is the golden rule for mastering any programming concept- practice makes perfect.

Now that you've mastered operators and their evaluation rules, you are all set for the next exciting step in your Python journey. In our next tutorial, we will explore conditionals and loops - the building blocks that make your programs truly intelligent and dynamic. Imagine being able to make your program decide what to do based on different conditions, or repeat tasks automatically. That’s what exactly we will be learning next. Stay Tuned.

Enjoyed today’s blog? Don’t forget to like, share it with your friends, and leave your valuable feedback – it really helps! Your support keeps the content coming.

Keep practicing and Happy Coding! 🚀

0
Subscribe to my newsletter

Read articles from Raj Patel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Raj Patel
Raj Patel

Hey there! I'm Raj Patel, a passionate learner and tech enthusiast currently diving deep into Data Science, AI, and Web Development. I enjoy turning complex problems into simple, intuitive solutions—whether it's building real-world ML projects like crop disease detection for farmers or creating efficient web platforms for everyday use. I’m currently sharpening my skills in Python, Machine Learning , Javascript and love sharing what I learn through blogs and open-source contributions. If you're into AI, clean code, or building impactful tech—let’s connect!