After completing this chapter, you will be able to:
- Understand Operator Precedence.
- Understand Associativity.
- Learn the order of evaluation of expressions.
- Write expressions using multiple operators.
- Avoid mistakes caused by incorrect operator precedence.
- Write efficient and readable Python programs.
In Python, an expression may contain multiple operators.
For example,
10 + 5 * 2Which operation is performed first?
- Addition?
- Multiplication?
Python follows predefined rules called Operator Precedence and Associativity to evaluate expressions correctly.
Understanding these rules helps programmers write accurate and error-free programs.
Operator Precedence determines which operator is evaluated first when an expression contains multiple operators.
Associativity determines the direction in which operators having the same precedence are evaluated.
The direction may be:
- Left to Right
- Right to Left
Operator precedence helps Python to:
- Evaluate expressions correctly.
- Avoid ambiguity.
- Produce consistent results.
- Reduce programming errors.
Highest precedence to lowest precedence.
| Precedence | Operators |
|---|---|
| Highest | () Parentheses |
** Exponent |
|
+x, -x, ~x Unary Operators |
|
*, /, //, % |
|
+, - |
|
<<, >> |
|
& |
|
^ |
|
| |
|
==, !=, >, <, >=, <= |
|
not |
|
and |
|
| Lowest | or |
Parentheses have the highest precedence.
result = (10 + 5) * 2
print(result)30
Without parentheses,
result = 10 + 5 * 2Output
20
The exponent operator has higher precedence than multiplication and addition.
print(2 ** 3 * 2)16
Explanation
2³ = 8
8 × 2 = 16
These operators have higher precedence than addition and subtraction.
print(10 + 4 * 5)30
Explanation
4 × 5 = 20
10 + 20 = 30
Addition and subtraction are evaluated after multiplication and division.
Example
print(30 - 5 + 2)Output
27
Operators with the same precedence are evaluated according to associativity.
Most operators follow Left to Right evaluation.
Example
print(20 - 5 - 3)Output
12
Explanation
20 - 5 = 15
15 - 3 = 12
Exponent (**) follows Right to Left evaluation.
Example
print(2 ** 3 ** 2)Output
512
Explanation
3² = 9
2⁹ = 512
Write a Python program to demonstrate operator precedence.
result = 10 + 5 * 2
print(result)20
result = (10 + 5) * 2
print(result)30
result = 2 ** 3 ** 2
print(result)512
result = 50 // 5 + 10 * 2 - 3
print(result)27
Explanation
50 // 5 = 10
10 × 2 = 20
10 + 20 = 30
30 - 3 = 27
Operator precedence is used in:
- Banking Applications
- Billing Systems
- Payroll Software
- Scientific Calculations
- Financial Software
- Engineering Applications
- Eliminates ambiguity.
- Produces correct results.
- Makes expressions easier to understand.
- Improves program accuracy.
result = 10 + 5 * 2Many beginners expect
30
Actual Output
20
2 ** 3 ** 2Correct evaluation
2 ** (3 ** 2)
Not
(2 ** 3) ** 2
Operator Precedence determines which operator is evaluated first in an expression.
Associativity determines the order of evaluation when operators have the same precedence.
Parentheses ().
Exponent operator (**).
Parentheses override the default operator precedence and improve readability.
- Parentheses have the highest precedence.
- Exponent (
**) is evaluated before multiplication. - Multiplication and division are evaluated before addition.
- Most operators follow Left to Right associativity.
- Exponent (
**) follows Right to Left associativity.
In this chapter, you learned:
- What Operator Precedence is.
- What Associativity is.
- Order of evaluation.
- Parentheses.
- Left-to-Right associativity.
- Right-to-Left associativity.
- Practical programs.
- Common mistakes.
- Interview questions.
In the next chapter, we will learn Operator Interview Questions and Answers, covering all operator types, output-based questions, frequently asked interview programs, and viva questions.