Python
The language quirks that bite in interviews - the places where Python's answer is correct, surprising, and not what the code was trying to say.
Truthiness
and / or return an operand, not a bool
and and or short-circuit and hand back one of the operands themselves. and returns the first falsy operand, otherwise the last one. or returns the first truthy operand, otherwise the last one.
This is what makes x = cache.get(k) or compute() work, and also what makes it silently wrong when 0 is a legitimate cached value.
& is bitwise, not logical
& operates on bits, not truthiness, and it only exists for types that implement it.
Note the asymmetry: bool & bool returns a bool, but mixing in an int returns an int. Reserve & and | for integers, sets, and NumPy/pandas arrays where they do elementwise work; use and / or / not for logic.
Falsy values by type
| Category | Falsy |
|---|---|
| Boolean | False |
| Null | None |
| Numbers | 0, 0.0, 0j, Decimal(0), Fraction(0, 1) |
| Sequences | '', (), [], range(0) |
| Mappings and sets | {}, set(), frozenset() |
Everything else is truthy, including "0", "False", [0], and [[]] - a container holding a falsy thing is still a non-empty container.
For your own classes, truthiness comes from __bool__(); if that is missing, Python falls back to __len__() != 0; if neither exists, the object is always truthy.