or — Expert Examples
Logical OR operator; returns True if at least one operand is true
or bytecode: jump-if-true
How 'or' compiles differently from 'and'.
python
import dis def or_expr(a, b): return a or b def or_chain(a, b, c): return a or b or c def mixed(a, b, c): return a and b or c print("a or b:") dis.dis(or_expr) print("\na or b or c:") dis.dis(or_chain) print("\na and b or c:") dis.dis(mixed) # 'or' uses POP_JUMP_IF_TRUE (opposite of 'and') # If left is truthy, keep it and skip right # 'and' uses POP_JUMP_IF_FALSE # If left is falsy, keep it and skip right
'or' compiles to POP_JUMP_IF_TRUE (skip right if left is truthy), while 'and' uses POP_JUMP_IF_FALSE (skip right if left is falsy). Mixed expressions chain these jumps together.
Want to try these examples interactively?
Open Expert Playground