The or keyword in Python is a logical operator that combines two expressions and returns the first operand that is truthy, or the last operand if none is. It does not always return True or False: it returns one of the operands themselves. This makes or useful both for conditions in if statements and for choosing default values.
Python evaluates or from left to right and uses short-circuit evaluation, so the right-hand side is only evaluated when the left-hand side is falsy.
or operator evaluates its operands
or in conditionsor, and where it goes wrongor
or operator evaluates its operandsThe expression a or b follows one rule: if a is truthy, the result is a and b is never evaluated. Otherwise the result is b, whatever its value.
In a boolean context, such as an if condition, you only care whether the result is truthy, so or behaves like logical OR. In an assignment, you get the actual operand back.
ora | b | a or b |
|---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Python treats these values as falsy: False, None, numeric zero (0, 0.0, 0j), empty sequences and collections ("", [], (), {}, set(), range(0)), and objects whose __bool__ returns False or whose __len__ returns 0. Everything else is truthy, including the string "0" and the string "False".
or in conditionsThe most common use is testing that at least one of several conditions holds.
Each comparison is evaluated in order. Once age >= 65 is true, has_pass is not looked at.
Because the right operand is skipped when the left is truthy, you can use or to avoid expensive or unsafe work.
slow_check() never runs. Put the cheapest or most likely test first. Do not rely on side effects in the right operand, since they may not happen.
or, and where it goes wrongA common idiom supplies a fallback when a value is empty.
This is only safe when every falsy value should be replaced. It misbehaves when 0, "" or [] are valid inputs.
When None is the only "missing" value, test for it explicitly.
orThis looks natural but is wrong.
The expression parses as (color == "red") or "green", and the non-empty string "green" is always truthy. Use in instead.
or with andand binds tighter than or, so a or b and c means a or (b and c). Add parentheses when you mix them so the intent is obvious.
or with |or is the logical operator and short-circuits. | is the bitwise OR (also set union and dictionary merge on dicts since Python 3.9). On booleans True | False works, but it evaluates both sides and does not short-circuit.
or return a boolean in Python?Not always. It returns one of its operands, so 0 or "x" gives "x". Wrap the result in bool() if you need a strict boolean.
or and |?or is a logical operator that short-circuits and works on truthiness. | is bitwise OR on integers and union on sets, and it always evaluates both operands.
or?or has lower precedence than and and not, and comparisons bind tighter than all three. So not a or b and c is (not a) or (b and c).
or in a list comprehension?Use it in the filter clause: [n for n in nums if n < 0 or n > 100]. For many alternatives, any(...) is often clearer.
or returns the first truthy operand, or the last operand when all are falsy, and it short-circuits so the right side may never run. Use it for "at least one of these" conditions, and use is None checks instead of or for defaults when 0 or an empty value is legitimate. Prefer in for comparing one value against several, and add parentheses when mixing or with and.