Chapter 02

Operators & Expressions

Floor division rounds toward -∞. Short-circuit evaluation returns values, not booleans. Chained comparisons work differently than you think. Test yourself.

Questions: 20
Format: Predict Output + Multiple Choice
Difficulty: ★★★☆☆
Key idea: Python operators often return the actual operand values, not just True or False. This surprises many C/Java programmers. Work through each one carefully.
Arithmetic Operators
Q 01Predict the Output
print(10 % 3)
print(-10 % 3)
print(10 % -3)
Output
1 2 -2
Why? Python's % (modulo) follows the sign of the divisor (the right operand). The result always satisfies: a == (a // b) * b + (a % b), where // floors toward negative infinity.

-10 % 3: -10 // 3 = -4, so -4 * 3 = -12, and -10 - (-12) = 2. In C, -10 % 3 = -1 — Python differs here.
Q 02Predict the Output
print(2 ** 3 ** 2)   # what is the associativity?
print((2 ** 3) ** 2)
print(2 ** (3 ** 2))
Output
512 64 512
Why? The ** operator is right-associative in Python. 2 ** 3 ** 2 evaluates as 2 ** (3 ** 2) = 2 ** 9 = 512. This is the mathematical convention for exponentiation. (2 ** 3) ** 2 = 8 ** 2 = 64 is different.
Q 03Multiple Choice
print(5 / 2 + 1)
print(5 // 2 + 1)
print(type(5 / 2 + 1))
print(type(5 // 2 + 1))
Answer: a) 3.5, 3, float, int.

5 / 2 is 2.5 (float). 2.5 + 1 is 3.5 (float + int = float).
5 // 2 is 2 (int). 2 + 1 is 3 (int + int = int). The type of the division operator determines the type chain.
Comparison & Chaining
Q 04Predict the Output
print(1 < 2 < 3)
print(1 < 2 > 1)
print(3 > 2 > 1 > 0)
print(1 == 1 == 1)
Output
True True True True
Why? Python supports chained comparisons. 1 < 2 < 3 means (1 < 2) and (2 < 3) — it checks all pairs in sequence. This is mathematically natural and differs from C, where 1 < 2 < 3 would evaluate left to right as (1 < 2) < 3, which is True < 3, which is 1 < 3 = True... accidentally the same but for wrong reasons.
Q 05Multiple Choice
print(1 < 3 > 2 < 4 > 3)
Answer: a) True.

This chain is: (1 < 3) and (3 > 2) and (2 < 4) and (4 > 3) = True and True and True and True = True. Python checks all consecutive pairs. Each individual comparison is True.
Logical Operators & Short-Circuit
Q 06Predict the Output
print(3 and 4)
print(0 and 4)
print(0 or 5)
print(None or False)
Output
4 0 5 False
Why? Python's and/or return one of the operand values, not necessarily True or False.

and: returns the first falsy value, or the last value if all are truthy. 3 and 4 → both truthy → returns last: 4. 0 and 4 → 0 is falsy → returns 0.
or: returns the first truthy value, or the last value if all are falsy. 0 or 5 → 0 is falsy → returns 5. None or False → both falsy → returns last: False.
Q 07Predict the Output
print("" or "default")
print("hello" or "default")
print(None or [] or "found")
print(False and 1/0)  # does this crash?
Output
default hello found False
Why? The last line is the key surprise. False and (1/0) does NOT crash! Python short-circuits: since False is already falsy, it returns False immediately without evaluating 1/0.

This is called short-circuit evaluation — it stops as soon as the result is known. Very useful in guard conditions: if x is not None and x.value > 0 — if x is None, the second part is never checked.
Q 08Multiple Choice
x = None
result = x or "fallback"
print(result)
Answer: b) "fallback".

None or "fallback" → None is falsy → returns "fallback". This is a very common Python idiom for default values: name = user_input or "Anonymous".

Caution: This pattern fails when 0, "", or [] are valid values — they are also falsy. In those cases, use if x is None: x = "fallback".
is vs == — Identity vs Equality
Q 09Multiple Choice
a = [1, 2, 3]
b = [1, 2, 3]
c = a

print(a == b)
print(a is b)
print(a is c)
Answer: a) True, False, True.

a == b: compares values — same elements → True.
a is b: compares identity — a and b are two separate list objects → False.
a is c: c = a makes c an alias for a → same object → True.

Rule: use == to compare values. Use is only for None, True, and False.
not, Negation
Q 10Predict the Output
print(not 0)
print(not 1)
print(not not 42)
print(not [])
print(not [0])
Output
True False True True False
Why? not always returns a proper boolean (True or False), unlike and/or. not 0 → True, not 1 → False. not not 42 double-negates → True. not [] → True (empty list is falsy). not [0] → False (non-empty list is truthy).
Bitwise Operators
Q 11Predict the Output
print(5 & 3)   # AND
print(5 | 3)   # OR
print(5 ^ 3)   # XOR
print(5 << 1)  # left shift
print(5 >> 1)  # right shift
Output
1 7 6 10 2
Why? In binary: 5 = 101, 3 = 011.
AND (101 & 011 = 001 = 1), OR (101 | 011 = 111 = 7), XOR (101 ^ 011 = 110 = 6).
5 << 1: shift left by 1 bit = multiply by 2 = 10. 5 >> 1: shift right by 1 = floor divide by 2 = 2.
Q 12Multiple Choice
print(True & False)
print(True | False)
print(True ^ True)
Answer: a) False, True, False.

Bitwise operators on booleans return booleans (since bool subclasses int). & is bitwise AND, | is bitwise OR. Unlike and/or, bitwise operators do NOT short-circuit — both sides are always evaluated.
Walrus Operator :=
Q 13Multiple Choice
nums = [1, 2, 3, 4, 5]
if (n := len(nums)) > 3:
    print(f"List is long: {n} items")
Answer: b) "List is long: 5 items".

The := walrus operator (Python 3.8+) assigns AND returns a value in a single expression. (n := len(nums)) assigns 5 to n and also uses that value in the comparison > 3. It avoids calling len() twice. Parentheses around the walrus expression are required here.
Q 14Predict the Output
data = [3, 7, 1, 9, 2]
filtered = [y for x in data if (y := x * 2) > 8]
print(filtered)
Output
[14, 18]
Why? The walrus operator inside a comprehension lets you compute a value, filter by it, AND use it in the output — all at once. y := x * 2 computes the doubled value. if y > 8 filters. The output uses y (already computed, not re-computed). 7*2=14 > 8 ✓, 9*2=18 > 8 ✓.
Operator Precedence
Q 15Predict the Output
print(2 + 3 * 4)
print(2 ** 3 + 1)
print(-2 ** 2)
print((-2) ** 2)
Output
14 9 -4 4
Why? The last two are the key surprises. -2 ** 2 is parsed as -(2 ** 2) = -4, NOT (-2) ** 2 = 4. Exponentiation ** has higher precedence than unary negation -. Always use parentheses when negating a base: (-2) ** 2 = 4.
Q 16Multiple Choice
print(not 1 + 1)
Answer: a) False.

Arithmetic (+) has higher precedence than not. So this parses as not (1 + 1) = not 2 = False. It does NOT parse as (not 1) + 1 = 0 + 1 = 1. When in doubt, use parentheses to make the intent explicit.
Augmented Assignment
Q 17Predict the Output
a = [1, 2]
b = a
a += [3]       # augmented assignment on list
print(a)
print(b)
print(a is b)
Output
[1, 2, 3] [1, 2, 3] True
Why? For lists, a += [3] calls a.__iadd__([3]) — it mutates a in place. So b also sees the change, and a is b remains True.

But for immutable types: x = 5; y = x; x += 1 → x is rebound to a new integer. y stays at 5. Augmented assignment behaves differently for mutable vs immutable objects.
Q 18Predict the Output
a = [1, 2]
b = a
a = a + [3]   # regular + creates a new list
print(a)
print(b)
print(a is b)
Output
[1, 2, 3] [1, 2] False
Why? a = a + [3] creates a brand new list and rebinds a to it. b still points to the original list. Compare with a += [3] (previous question) which mutates in place. This difference between += and = x + ... on mutable types is a frequent source of bugs.
Q 19Multiple Choice
print(4 < 5 == True)
Answer: b) False.

This is a chained comparison: (4 < 5) and (5 == True). The first part 4 < 5 is True. The second part 5 == True: True == 1, so 5 == 1 → False. Combined: True and False = False.

The trap: people expect 4 < 5 == True to read as (4 < 5) == True which would be True == True = True. But chaining works differently.
Q 20Predict the Output
x = 5
print(x := x + 1)  # walrus in print
print(x)
Output
6 6
Why? := inside print() is a valid expression. It evaluates x + 1 = 6, assigns it to x, and returns 6, which is printed. After the line, x is 6. The walrus operator can be used anywhere an expression is expected — in function calls, comprehensions, conditionals, etc.
← Chapter 01: Data Types Chapter 03: Control Flow →