Best afterTruthiness: what counts as true
and / or / not — and what or returns
Reading x == 1 or 2 as 'x is 1 or 2' is the classic boolean trap. Python groups it as (x == 1) or 2, and or hands back an operand, not always a tidy True/False. Watch what actually lands in match.
Check whether x (which is 5) is equal to 1 or 2.
x == 1 or 2 is always truthy execution-derived · CPython
variables
state after line 1 runs
| variable | value |
|---|---|
x | 5 |
step 1 / 2
Click a line, drag the slider, or use the ← → keys.
What you are looking at
Watch match closely — the value may not even be a boolean.
- Buggy —
match = x == 1 or 2becomesFalse or 2, andorreturns its first truthy operand: the integer2. NotTrue— the number 2, which is truthy, so anyif match:would always fire. - Procedural —
match = x == 1 or x == 2compares against both values in full and gives a realFalse. Each side of anorneeds to be a complete condition.
Every state you see came from running the program under CPython's tracer at build time — see how GlassBox stays honest.