Operator precedence tells us which math or logic operation happens first in a calculation. Evaluation order is the sequence in which parts of the expression are solved.
Operator precedence and evaluation order in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
result = expression_with_operators
Operators like *, / have higher precedence than +, - so they run first.
Use parentheses () to change the order and make your code clearer.
Examples
Python
result = 2 + 3 * 4
Python
result = (2 + 3) * 4
Python
result = 10 / 2 * 3
Python
result = True or False and False
Sample Program
This program shows how operator precedence and evaluation order affect the results of expressions.
Python
a = 5 + 2 * 3 b = (5 + 2) * 3 c = 10 / 2 * 4 d = True or False and False print(f"a = {a}") print(f"b = {b}") print(f"c = {c}") print(f"d = {d}")
Important Notes
Remember that multiplication and division happen before addition and subtraction.
When operators have the same precedence, Python evaluates them from left to right.
Use parentheses to make your code easier to read and avoid mistakes.
Summary
Operator precedence decides which operations run first.
Evaluation order is left to right for operators with the same precedence.
Parentheses can change the order and improve clarity.
Practice
1. Which operator has the highest precedence in the expression
3 + 4 * 2?easy
Solution
Step 1: Recall operator precedence rules
Multiplication (*) has higher precedence than addition (+), subtraction (-), and division (/).Step 2: Identify highest precedence operator in expression
In3 + 4 * 2, multiplication (*) runs before addition (+).Final Answer:
Multiplication (*) -> Option AQuick Check:
Highest precedence = Multiplication (*) [OK]
Hint: Multiplication and division run before addition and subtraction [OK]
Common Mistakes:
- Thinking addition runs before multiplication
- Ignoring operator precedence
- Assuming left to right always applies
2. Which of the following expressions is syntactically correct in Python?
easy
Solution
Step 1: Check each expression for syntax errors
5 + * 3 has two operators in a row without operand: invalid.
4 + (3 * 2) uses parentheses correctly and valid operators.
7 / / 2 has double division operator which is invalid.
8 - - ends with operator without operand: invalid.Step 2: Confirm correct syntax
Only 4 + (3 * 2) is syntactically correct:4 + (3 * 2).Final Answer:
4 + (3 * 2) -> Option BQuick Check:
Valid syntax = 4 + (3 * 2) [OK]
Hint: Check for missing operands or extra operators [OK]
Common Mistakes:
- Using two operators in a row
- Missing parentheses around expressions
- Ending expression with an operator
3. What is the output of the following code?
result = 10 - 3 * 2 + 4 // 2 print(result)
medium
Solution
Step 1: Apply operator precedence and evaluate multiplication and floor division first
3 * 2 = 6
4 // 2 = 2Step 2: Evaluate the expression left to right with addition and subtraction
10 - 6 + 2 = 4 + 2 = 6Final Answer:
6 -> Option CQuick Check:
10 - 6 + 2 = 6 [OK]
Hint: Multiply and divide before add and subtract [OK]
Common Mistakes:
- Adding before multiplying
- Using normal division instead of floor division
- Ignoring left to right evaluation for same precedence
4. Find the error in this expression:
value = 5 + (3 * 2
medium
Solution
Step 1: Check parentheses balance
Expression has an opening parenthesis '(' but no matching closing parenthesis ')'.Step 2: Identify syntax error
Missing closing parenthesis causes syntax error in Python.Final Answer:
Missing closing parenthesis -> Option DQuick Check:
Parentheses must be balanced [OK]
Hint: Count opening and closing parentheses carefully [OK]
Common Mistakes:
- Ignoring missing parentheses
- Assuming expression is valid without closing parenthesis
- Confusing operator errors with syntax errors
5. Given the expression
result = (2 + 3) * (4 - 1) ** 2 // 5, what is the value of result?hard
Solution
Step 1: Evaluate parentheses and exponentiation first
(2 + 3) = 5
(4 - 1) = 3
3 ** 2 = 9Step 2: Multiply and then floor divide
5 * 9 = 45
45 // 5 = 9Final Answer:
9 -> Option AQuick Check:
Parentheses and exponent first, then multiply, then floor divide [OK]
Hint: Do parentheses and powers before multiply/divide [OK]
Common Mistakes:
- Ignoring exponentiation precedence
- Dividing before multiplying
- Not applying floor division correctly
