PRECEDENCE EXAMPLE
PRECEDENCE EXAMPLE
x = 2 + 3 * 4 # Multiplication has higher precedence than addition
print(x) # Output: 14
y = (2 + 3) * 4 # Parentheses enforce addition to be evaluated first
print(y) # Output: 20
z = 2 ** 3 ** 2 # Exponentiation is right associative
print(z) # Output: 512
a = 9 % 4 + 2 / 3 # Modulo has higher precedence than division
print(a) # Output: 2.6666666666666665
b = (9 % 4) + (2 / 3) # Parentheses enforce separate evaluations
print(b) # Output: 2.6666666666666665
c = 10 / 2 * 3 # Multiplication and division have the same precedence
print(c) # Output: 15.0
d = 10 / (2 * 3) # Parentheses enforce multiplication to be evaluated first
print(d) # Output: 1.6666666666666667
e = not False or True # Logical NOT has higher precedence than logical OR
print(e) # Output: True
f = False or True and True # Logical AND has higher precedence than logical OR
print(f) # Output: True
g = False or (True and True) # Parentheses enforce separate evaluations
print(g) # Output: False
h = 5 > 2 and 3 <= 7 # Comparison operators have higher precedence than logical operators
print(h) # Output: True
i = 3 + 4 * 5 >= 17 or not (4 != 4) # Complex expression with multiple operators
print(i) # Output: True
j = 2 << 3 | 1 # Bitwise shift has higher precedence than bitwise OR
print(j) # Output: 17
k = 10 in [1, 2, 3, 10] # Membership operator
print(k) # Output: True
l = 10 is not None # Identity operator
print(l) # Output: True
m = 5
m += 2 * 3 # Compound assignment operator
print(m) # Output: 11
Comments
Post a Comment