Operator Precedence
Operator Precedence
In Python, operator precedence determines the order in which operators are evaluated
in an expression. Operators with higher precedence are evaluated before operators
with lower precedence.
Associativity:
Associativity determines the order of evaluation when two operators of the same
precedence appear in an expression. Operators can have left-to-right or right-to-left
associativity.
All operators in the above given table except **, logical not have left to right
associativity.
** and logical not has right to left associativity
Example: left to right
print(10 / 5 * 2)
/ and * have the same precedence.
Since they follow left-to-right associativity:
Output: 4.0
3 ** 2 → 9
2 ** 9 → 512
Output: 512
CBSE 12th Board questions:
1. not ((True and False) or True)
Ans: False
2. print(True or not True and False)
ANs : true
3. What will be the output of the following expression?
24//6%3 , 24//4//2 , 48//3//4
Ans: 1,3,4
4. print(25 // 4 + 3**1**2 * 2)
Ans : 12
5. print(2**3 + (5 + 6)**(1 + 1))
ANs : 129
6. print(16-(3+2)*5+2**3*4)
Ans: 23
7. 8 and 13 > 10
Ans : True
8. 7%5==2 and 4%2>0 or 15//2==7.5
Ans: False