One of the most important concepts in any programming language for beginners is operator precedence and associativity. This is the foundation of logical reasoning in any programming language and significantly impacts how expressions are evaluated with multiple operators and values. Let me teach you this crucial concept because if you master these, you will avoid logical errors, build efficient programmes, and even debug code for logical errors.
Table of Contents
What is Operator Precedence?
Operator precedence determines the order of execution for different operators in an expression. For example:
Operator precedence tells us which operation executes first when there are multiple operators in an expression.
2 + 2 / 2
PythonFirstly, you might calculate
2 + 2 = 4
and then4 / 2 = 2.0
But the correct answer is 3
. Why? Because of the BODMAS (Brackets, Orders, Division, Multiplication, Addition, Subtraction) rule:
- First, division happens:
2 / 2 = 1
. - Then, addition:
2 + 1 = 3
.
Similarly, in programming, every operator has a priority, and this priority is called precedence. I personally recommend you to learn below the table.
Operator(s) | Description | Associativity |
---|---|---|
() | Parentheses | Left-to-Right |
** | Exponentiation (Power) | Right-to-Left |
* , / , // , % | Multiplication, Division, Floor division, Modulus | Left-to-Right |
+ , - | Addition, Subtraction | Left-to-Right |
<<,>> | Shifts | Left-to-Right |
& | Bitwise AND | Left-to-Right |
^ | Bitwise XOR | Left-to-Right |
| | Bitwise OR | Left-to-Right |
== , != , < , <= , > , >= | Comparison Operators | Left-to-Right |
is , is not , in , not in | Identity and Membership operators | Left-to-Right |
not | Logical NOT | Right-to-Left |
and , or | Logical AND and OR | Left-to-Right |
= , += , -= , *= , /= , %= | Logical AND and OR | Left-to-Right |
In the table, precedence goes from top to bottom; hence, the operator mentioned at the top has the highest precedence (solve first). If two operators have the same precedence, use associativity. But what is associativity?
What is Operator Associativity?
Associativity defines the order in which operators of the same precedence are evaluated. For example, in the expression (3 + 5 - 2)
, both addition and subtraction have the same precedence, so we evaluate from left to right according to the table. Some operators, like exponentiation (**
) and assignment (=
), follow right to left rule.
for example:
2 ** 3 ** 2
PythonThis means 3 ** 2
(9
) is evaluated first, and then 2 ** 9
= 512
.
Some other Examples
Example 1
2 + 6 * 3 ** 2 / 2 - (6 + 8)
PythonSolution:
- Based on precedence, parentheses are resolved first.
(6 + 8) = 14
, Hence the expression is now2 + 6 * 3 ** 2 / 2 - 14
- The next highest precedence is for
**
(right-to-left associativity).3 ** 2 = 9
, Hence the expression becomes2 + 6 * 9 / 2 - 14
- Both
*
and/
have the same precedence and are evaluated left-to-right.3.1. First, perform multiplication:6 * 9 = 54
, Hence the expression become2 + 54 / 2 - 14
.
3.2. Next, perform division:54 / 2 = 27.0
, Hence the expression become2 + 27.0 - 14
+
and-
have the same precedence and are evaluated left-to-right.
4.1. First, perform addition:2 + 27.0 = 29.0
, Hence the expression is now29.0 - 14
.
4.2. Next, perform subtraction:29.0 - 14 = 15.0
. Hence 15.0 is the answer.
Example 2
(3 + 2 ** 3 > 20 or 5 * 2 < 15) and not (10 / 2 == 5)
Python- Bracket: As we have brackets on both sides between
and not
, we use associativity. - Exponentiation of first bracket:
2 ** 3 = 8
- Arithmetic:
3 + 8 = 11
,5 * 2 = 10
,10 / 2 = 5.0
- Comparison:
11 > 20 = False
,10 < 15 = True
,5.5 == 5 = True
- Logical NOT:
not True = False
- Logical OR:
False or True = True
- Logical AND:
True and False = False
Conclusion
Understanding these rules helps build a solid foundation for tackling more complex problems. I recommend practicing with platforms that provide precedence-related problems to strengthen your logical thinking.
Thank you for reading, and I hope this post helps clarify these important concepts! If you have any questions or need further clarification, feel free to ask in the comments below.
H.W.
Before ending, I want you to solve some of the questions to test your understanding of operator.
# 1
5 + 3 * 2 - (8 / 4)
# 2
4 * 3 ** 2 / 2 + (6 - 3) > 15 and 10 % 3 == 1
# 3
(10 + 2 ** 3 < 50) or (20 / 5 * 3 == 12) and not (15 % 4 > 1)
# 4
not (3 * 4 + 2 ** 2 < 20) and (7 % 3 == 1 or 5 * 2 != 10)
# 5
((3 + 5) ** 2 / 4 < 50) or not (6 * 2 > 10 and 15 // 4 == 3)
PythonIf you want to practice more problems, you can visit this website