Operator Precedence and Expression Evaluation in Python Class 11 CBSE Computer Science
Class 11 · Computer Science
Operator Precedence and Expression Evaluation (CBSE Class 11 Computer Science)
An expression may contain multiple operators. Python follows a predefined order, called operator precedence, to decide which operation should be performed first. Understanding operator precedence helps programmers write correct expressions and predict program output accurately.
Learning Objectives
- Understand Operator Precedence.
- Learn Associativity.
- Understand Expression Evaluation.
- Use Parentheses to control evaluation.
- Predict program output correctly.
What is Operator Precedence?
Operator Precedence is the priority assigned to operators. Operators with higher precedence are evaluated before operators with lower precedence.
Why is Operator Precedence Important?
Without operator precedence, the result of an expression containing multiple operators would be ambiguous. Python uses precedence rules to ensure expressions are evaluated consistently.
Operator Precedence Table
The following table lists the operators covered in the CBSE Class 11 syllabus in decreasing order of precedence (highest to lowest).
| Precedence | Operators | Description |
|---|---|---|
| Highest | () | Parentheses |
| 2 | ** | Exponentiation |
| 3 | +, -, (Unary) | Unary Plus, Unary Minus |
| 4 | *, /, //, % | Multiplication, Division, Floor Division, Modulus |
| 5 | +, - | Addition, Subtraction |
| 6 | <, <=, >, >=, ==, != | Relational Operators |
| 7 | not | Logical NOT |
| 8 | and | Logical AND |
| Lowest | or | Logical OR |
Expression Evaluation
Python evaluates an expression according to operator precedence. If operators have the same precedence, associativity determines the order of evaluation.
Example 1
5 + 3 * 2
Evaluation
= 5 + (3 × 2)
= 5 + 6
= 11
Example 2
20 - 8 // 2
Evaluation
= 20 - 4
= 16
Example 3
2 + 3 * 4 ** 2
Evaluation
= 2 + 3 * 16
= 2 + 48
= 50
Using Parentheses
Parentheses have the highest precedence. They are used to change the default order of evaluation and improve readability.
Example
(5 + 3) * 2
Evaluation
= 8 × 2
= 16
Without Parentheses vs With Parentheses
| Expression | Result |
|---|---|
| 5 + 3 * 2 | 11 |
| (5 + 3) * 2 | 16 |
Associativity
Associativity decides the order of evaluation when two operators have the same precedence.
- Most arithmetic operators are evaluated from left to right.
- The exponent operator (
**) is evaluated from right to left.
Example (Left to Right)
20 / 5 * 2
= 4 * 2
= 8
Example (Right to Left)
2 ** 3 ** 2
= 2 ** (3 ** 2)
= 2 ** 9
= 512
Dry Run Example
Program
a = 8
b = 4
c = a + b * 2 - 3
print(c)
Dry Run Table
| Step | Operation | Result |
|---|---|---|
| 1 | b * 2 | 8 |
| 2 | a + 8 | 16 |
| 3 | 16 - 3 | 13 |
| 4 | print(c) | 13 |
Think Like a Programmer
Predict the output without executing the program.
a = 5
b = 2
print(a + b * 3)
print((a + b) * 3)
print(a ** b)
print(15 % 4)
Click to View Answer
11
21
25
3
Output Prediction Challenge
x = 10
y = 4
print(x // y + 3)
print(x > y and y < 5)
print(not(x == 10))
Click to View Answer
5
True
False
Real-Life Example
Imagine solving a mathematics expression:
5 + 3 × 2
You first perform multiplication and then addition according to the BODMAS rule. Python follows a similar precedence rule when evaluating expressions.
Common Beginner Mistakes
- Ignoring operator precedence.
- Using unnecessary parentheses.
- Assuming operations are always evaluated from left to right.
- Forgetting that
**is evaluated from right to left. - Confusing
/with//.
Memory Trick
Remember the order:
()
**
*, /, //, %
+, -
Comparison
not
and
or
Exam Tips
- Always evaluate expressions step by step.
- Use parentheses whenever the intended order is not obvious.
- Practice output-based questions regularly.
- Remember that
**is right-associative. - Know the precedence of arithmetic, relational, and logical operators.
CBSE Important Questions
2 Marks
- What is operator precedence?
- What is associativity?
3 Marks
- Evaluate the following expression with proper steps:
5 + 6 * 2 - 8 // 2 - Differentiate between operator precedence and associativity.
5 Marks
- Explain operator precedence in Python with suitable examples and dry runs.
Practice Yourself
- Find the output of
8 + 4 * 2. - Evaluate
(8 + 4) * 2. - Predict the output of
2 ** 3 ** 2. - Write three expressions using parentheses and evaluate them.
- Explain the role of parentheses in expression evaluation.
Frequently Asked Questions (FAQs)
1. What is operator precedence?
Operator precedence is the order in which Python evaluates operators in an expression.
2. Which operator has the highest precedence?
Parentheses ().
3. Which operator is right-associative?
The exponent operator (**).
4. Why should parentheses be used?
They improve readability and allow programmers to control the order of evaluation.
5. What is associativity?
Associativity determines the order in which operators of the same precedence are evaluated.
Summary
- Operator precedence determines the order in which operators are evaluated.
- Parentheses have the highest precedence.
- Most operators are evaluated from left to right, while
**is evaluated from right to left. - Parentheses can change the default order of evaluation.
- Understanding precedence helps programmers write accurate expressions and predict program output correctly.