Floor division rounds toward -∞. Short-circuit evaluation returns values, not booleans. Chained comparisons work differently than you think. Test yourself.
True or False. This surprises many C/Java programmers. Work through each one carefully.
print(10 % 3) print(-10 % 3) print(10 % -3)
% (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.
print(2 ** 3 ** 2) # what is the associativity? print((2 ** 3) ** 2) print(2 ** (3 ** 2))
** 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.
print(5 / 2 + 1) print(5 // 2 + 1) print(type(5 / 2 + 1)) print(type(5 // 2 + 1))
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.
print(1 < 2 < 3) print(1 < 2 > 1) print(3 > 2 > 1 > 0) print(1 == 1 == 1)
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.
print(1 < 3 > 2 < 4 > 3)
(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.
print(3 and 4) print(0 and 4) print(0 or 5) print(None or False)
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.
print("" or "default") print("hello" or "default") print(None or [] or "found") print(False and 1/0) # does this crash?
False and (1/0) does NOT crash! Python short-circuits: since False is already falsy, it returns False immediately without evaluating 1/0.if x is not None and x.value > 0 — if x is None, the second part is never checked.
x = None result = x or "fallback" print(result)
None or "fallback" → None is falsy → returns "fallback". This is a very common Python idiom for default values: name = user_input or "Anonymous".0, "", or [] are valid values — they are also falsy. In those cases, use if x is None: x = "fallback".
a = [1, 2, 3] b = [1, 2, 3] c = a print(a == b) print(a is b) print(a is c)
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.== to compare values. Use is only for None, True, and False.
print(not 0) print(not 1) print(not not 42) print(not []) print(not [0])
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).
print(5 & 3) # AND print(5 | 3) # OR print(5 ^ 3) # XOR print(5 << 1) # left shift print(5 >> 1) # right shift
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.
print(True & False) print(True | False) print(True ^ True)
bool subclasses int). & is bitwise AND, | is bitwise OR. Unlike and/or, bitwise operators do NOT short-circuit — both sides are always evaluated.
nums = [1, 2, 3, 4, 5] if (n := len(nums)) > 3: print(f"List is long: {n} items")
:= 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.
data = [3, 7, 1, 9, 2] filtered = [y for x in data if (y := x * 2) > 8] print(filtered)
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 ✓.
print(2 + 3 * 4) print(2 ** 3 + 1) print(-2 ** 2) print((-2) ** 2)
-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.
print(not 1 + 1)
+) 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.
a = [1, 2] b = a a += [3] # augmented assignment on list print(a) print(b) print(a is b)
a += [3] calls a.__iadd__([3]) — it mutates a in place. So b also sees the change, and a is b remains True.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.
a = [1, 2] b = a a = a + [3] # regular + creates a new list print(a) print(b) print(a is b)
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.
print(4 < 5 == True)
(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.4 < 5 == True to read as (4 < 5) == True which would be True == True = True. But chaining works differently.
x = 5 print(x := x + 1) # walrus in print print(x)
:= 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.