Understanding the `or` Operator in Python
Daniel Hayes
Full-Stack Engineer · Leapcell

Key Takeaways
- The
or
operator returnsTrue
if at least one condition is true. - It uses short-circuit evaluation and skips the second condition if the first is
True
. - With non-boolean values,
or
returns the first truthy or last falsy value.
In Python, the or
operator is a logical operator used to combine two conditions. It returns True
if at least one of the conditions is true; otherwise, it returns False
. This behavior is fundamental in controlling the flow of programs and making decisions based on multiple conditions.
Basic Usage of the or
Operator
The syntax for using the or
operator is straightforward:
condition1 or condition2
Here, condition1
and condition2
are expressions that evaluate to either True
or False
. The or
operator evaluates these conditions and returns True
if at least one of them is True
. If both are False
, it returns False
.
Example:
x = 10 y = 5 if x > 5 or y > 10: print("At least one condition is True") else: print("Both conditions are False")
In this example, the first condition x > 5
is True
, and the second condition y > 10
is False
. Since one of the conditions is True
, the or
operator returns True
, and the output will be:
At least one condition is True
Short-Circuit Evaluation
Python's or
operator employs a technique called short-circuit evaluation. This means that the evaluation stops as soon as the result is determined. If the first condition is True
, Python does not evaluate the second condition because the overall result will definitely be True
.
Example:
def check_first(): print("Checking first condition") return True def check_second(): print("Checking second condition") return False if check_first() or check_second(): print("At least one condition is True")
Output:
Checking first condition
At least one condition is True
In this example, check_first()
returns True
, so check_second()
is never called, demonstrating short-circuit evaluation.
Using or
with Non-Boolean Values
In Python, the or
operator can also be used with non-boolean values. It returns the first truthy value it encounters or the last value if none are truthy. A value is considered truthy if it evaluates to True
in a boolean context.
Example:
a = '' b = 'Hello' result = a or b print(result) # Outputs: Hello
Here, a
is an empty string, which is falsy, and b
is a non-empty string, which is truthy. The or
operator returns b
since a
is falsy.
This behavior is particularly useful for setting default values.
Example:
user_input = '' default_value = 'Default' final_value = user_input or default_value print(final_value) # Outputs: Default
In this case, since user_input
is an empty string (falsy), final_value
is assigned the default_value
.
Common Pitfalls
While the or
operator is powerful, it's essential to be aware of its behavior to avoid common pitfalls. One such pitfall is assuming that or
always returns a boolean value. As seen earlier, when used with non-boolean values, or
returns the first truthy value or the last value if none are truthy.
Example:
result = 0 or 'No data' print(result) # Outputs: No data
Here, 0
is falsy, so the or
operator returns 'No data'
, which is a string, not a boolean.
Understanding the or
operator's behavior with different data types and its short-circuit evaluation is crucial for writing efficient and bug-free Python code.
FAQs
No, when used with non-boolean values, it returns the first truthy or last falsy value.
It stops evaluating conditions once the result is known, often skipping the second condition if the first is true.
If the first value is falsy, or
returns the second, making it useful for fallbacks.
We are Leapcell, your top choice for hosting Python projects.
Leapcell is the Next-Gen Serverless Platform for Web Hosting, Async Tasks, and Redis:
Multi-Language Support
- Develop with Node.js, Python, Go, or Rust.
Deploy unlimited projects for free
- pay only for usage — no requests, no charges.
Unbeatable Cost Efficiency
- Pay-as-you-go with no idle charges.
- Example: $25 supports 6.94M requests at a 60ms average response time.
Streamlined Developer Experience
- Intuitive UI for effortless setup.
- Fully automated CI/CD pipelines and GitOps integration.
- Real-time metrics and logging for actionable insights.
Effortless Scalability and High Performance
- Auto-scaling to handle high concurrency with ease.
- Zero operational overhead — just focus on building.
Explore more in the Documentation!
Follow us on X: @LeapcellHQ