or operator

Updated March 16, 2026 · Keywords
keyword boolean operator logical

The or keyword is a logical disjunction operator in Python. It returns the first truthy value it finds, or the last value if none are truthy.

Syntax

result = left_operand or right_operand

How It Works

Python’s or doesn’t just return True or False — it returns one of its operands:

  1. If the left operand is truthy, return it immediately (short-circuit)
  2. If the left operand is falsy, evaluate and return the right operand

This behavior is called short-circuit evaluation.

# First is truthy - returns it immediately
print(True or False)
# True

print(1 or 0)
# 1

print("hello" or "world")
# hello

# First is falsy - returns the second value
print(False or True)
# True

print(0 or 1)
# 1

print("" or "hello")
# hello

# Both falsy - returns the last value
print(False or 0 or "")
# ""

print(None or False or 0)
# 0

Common Use Cases

Default values

The most common use for or is providing fallback values:

# User didn't provide a name - use a default
username = ""
display_name = username or "Guest"
print(display_name)
# Guest

# Configuration with defaults
config = {"port": None}
port = config.get("port") or 8080
print(port)
# 8080

# Function parameters with defaults
def greet(name=None):
    return f"Hello, {name or 'Stranger'}!"

print(greet())
# Hello, Stranger!

print(greet("Alice"))
# Hello, Alice!

Short-circuit for early returns

def get_user(user_id, database):
    user = database.fetch(user_id) or None
    if not user:
        raise ValueError(f"User {user_id} not found")
    return user

Conditional execution

debug_mode = True
verbose = False

# Only sets if debug_mode is truthy
setting = debug_mode or verbose

print(setting)
# True

# Useful for feature flags
premium_user = False
trial_user = True

# premium_user is checked first
access_level = premium_user or trial_user or False
print(access_level)
# True

Chaining multiple values

# Find the first non-empty value
result = "" or None or "found" or "default"
print(result)
# found

# Useful for configuration chains
env_var = ""
config_file = ""
default = "localhost:8000"

server = env_var or config_file or default
print(server)
# localhost:8000

Guard Clauses

Use or to guard against falsy values:

def process_data(data):
    # Early return if no data
    data = data or return_default()
    
    # Continue processing
    return transform(data)

def return_default():
    print("Using default data")
    return {"items": []}

process_data(None)
# Using default data

process_data({"items": [1, 2, 3]})
# (no output - used provided data)

Short-Circuit Demonstration

The right operand is never evaluated if the left is truthy:

def risky():
    print("This function was called!")
    return True

result = True or risky()
# Nothing printed - risky() was never called

result = False or risky()
# This function was called!
# True

This is useful for avoiding errors:

numbers = [1, 2, 3]

# Safe - doesn't try to access index 10
result = len(numbers) > 10 or numbers[10]
# [1, 2, 3] (short-circuits because first is truthy)

# Without short-circuit, this would raise IndexError:
# result = numbers[10] or len(numbers) > 10  # IndexError!

Boolean Context

When used in a boolean context (like if), Python treats the result as a boolean:

if "hello" or []:
    print("At least one is truthy")
# At least one is truthy

if "" or 0 or None:
    print("Won't print")
# (nothing printed - all are falsy)

Difference from and

Expressionand resultor result
1 or 001
0 or 101
True or FalseFalseTrue
False or TrueFalseTrue

Common Pitfalls

Confusing with || in other languages

In JavaScript, || always returns a boolean. Python’s or returns the actual value:

# Python
result = 0 or "default"
print(result)
# "default"

# JavaScript
// const result = 0 || "default";
// Returns "default" - similar behavior, but returns boolean in some contexts

Using or with 0 or ""

Be careful — 0 and "" are falsy, so they won’t be selected even if they’re valid:

# Intended: use 0 if provided
value = 0
result = value or 10
print(result)
# 10 — Oops! 0 is falsy

# Fix: use explicit None check
value = 0
result = value if value is not None else 10
print(result)
# 0 — Correct

See Also