or

Either suffices — and because it returns operands, `x or default` is Python’s classic fallback idiom.

Logical operatorPython 1.0+Live demo
Common call
name = raw or "anonymous"
Returns
first truthy operand, else the last operand
Replaces
"" or "default" is 'default'
Watch out
falsy-but-valid values (0, "", []) get replaced too
aaLeft operand — returned when truthy; b never runs then.type: Any · required or bbRight operand — the fallback.type: Any · required
Any

Demo

Live evaluation
Try:
Inputs
aAnyempty = falsy
bAnyfallback
Output
'value' or 'default'
'value'

Truthy a → a itself comes back and b is never evaluated. Falsy a (empty string) → b, whatever it is. That operand-returning behavior is what makes the `x or default` idiom work.

Operands

NameTypeRequiredDescription
aAnyyesLeft operand — returned when truthy; b never runs then.
bAnyyesRight operand — the fallback.

Return value

Anya when a is truthy, otherwise b — an OPERAND, not necessarily a bool. b is never evaluated when a is truthy.

Common patterns

Default values
The classic fallback — with the falsy caveat below.
display_name = user.nickname or user.username
First truthy of several
Chains left to right, stops at the first truthy value.
config = cli_arg or env_var or DEFAULT

Examples

1. Truthy left wins
"value" or "default"
Returns
'value'
2. Falsy left falls back
"" or "default"
Returns
'default'
3. Chained fallbacks
0 or "" or "last"
Returns
'last'

Pitfalls

1. Falsy-but-valid values get replaced
0, "", and [] are legitimate data — or cannot tell them from missing.
Data lost
port = config_port or 8080
# config_port = 0 → 8080!
explicit 0 silently replaced
None-aware
port = config_port if config_port is not None else 8080
0 preserved
2. Side effects on the right may never run
Short-circuiting skips b when a is truthy.
Skipped call
cache.get(k) or fetch(k)   # fetch skipped on hit — intended?
depends — make it explicit
Explicit
v = cache.get(k)
if v is None:
    v = fetch(k)
intent visible

When to use

Use it
  • Fallback defaults where falsy = missing
  • First-truthy-wins chains
  • Combined conditions in if/while
Reach for something else
  • 0/""/[] are valid data → explicit `is None` check
  • Element-wise boolean ops on arrays → | (numpy/pandas)

Notes

Complexity
O(1) plus operand evaluation
Return
an operand — a if truthy, else b
CPython impl
Compiled to JUMP_IF_TRUE_OR_POP — not a dunder method
Memory
No allocation
Thread-safe
Depends only on the operand expressions

FAQ

No. `x or default` replaces ALL falsy values; the None-only equivalent is the conditional expression: x if x is not None else default.

History

1.0
Core operator from the beginning.