Operator Precedence and Binding Flashcards
7 cards from real PCEP practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Operator Precedence and Binding flashcards as text
What is the result of `-(2 ** 2)`?
Answer: -4
Exponentiation `**` has higher precedence than unary minus, so `2**2=4` first, then `-4`.
Which expression is evaluated first in `a = 1 + 2 * 3 ** 1 - 4 // 2`?
Answer: 3 ** 1
Exponentiation `**` has the highest precedence among arithmetic operators, so `3**1` is evaluated first.
What is the value of `5 - 2 - 1`?
Answer: 2
Subtraction is left-associative: `(5-2)-1 = 3-1 = 2`.
What does `1 < 2 < 3` evaluate to in Python?
Answer: True
Python supports chained comparisons; `1 < 2 < 3` is equivalent to `(1 < 2) and (2 < 3)` which is True.
What is the result of `8 / 2 * 2`?
Answer: 8.0
`/` and `*` have the same precedence and are left-associative: `(8/2)*2 = 4.0*2 = 8.0`.
In Python, what does `not 0 == 0` evaluate to?
Answer: False
Comparison `==` has higher precedence than `not`, so `0==0` is True first, then `not True` equals False.
What is the result of `2 ** -1`?
Answer: 0.5
Python evaluates `2 ** -1` as `1/2 = 0.5` since `**` accepts negative exponents.