Table of contents
1.
Introduction
2.
What is the OR Operator?
3.
Syntax of the OR Operator
4.
Using the OR Operator in Conditional Statements
5.
Example 1: Basic Usage with Boolean Values
5.1.
Python
6.
Example 2: Combining Conditions with Integers
6.1.
Python
7.
Example 3: Using OR with Strings
7.1.
Python
8.
Short-Circuit Evaluation
8.1.
Example 
8.2.
Python
9.
Frequently Asked Questions
9.1.
What is the difference between | and or in Python?
9.2.
How does the OR operator handle non-boolean values?
9.3.
Can OR be used in list comprehensions?
9.4.
What is the precedence of the OR operator?
10.
Conclusion
Last Updated: Aug 11, 2025
Easy

Python OR Operator

Introduction

In Python, logical operators are used to combine conditional statements. One of these operators is the OR operator. It is a crucial part of writing effective conditions in your code. The OR operator allows you to check multiple conditions and execute code based on whether at least one of the conditions is true. 

Python OR Operator

In this article, we'll explore how the OR operator works, its syntax, and provide examples to help you understand its use.

What is the OR Operator?

The OR operator in Python is used to evaluate multiple conditions. If any one of the conditions is true, the OR operator returns True. If all conditions are false, it returns False. This makes the OR operator useful for checking if at least one of several conditions holds true.

Syntax of the OR Operator

The syntax for using the OR operator is straightforward. You use the keyword or between two or more conditions.

condition1 or condition2


You can use this in if statements, loops, and other conditional constructs.

Using the OR Operator in Conditional Statements

The OR operator is often used in if statements to combine multiple conditions. Here’s how it works:

if condition1 or condition2:
    # Execute this code if at least one condition is true

Example 1: Basic Usage with Boolean Values

Let's start with a simple example where we check two conditions using the OR operator.

  • Python

Python

x = 10

y = 5

if x > 5 or y > 10:

   print("At least one condition is true.")

else:

   print("Both conditions are false.")
You can also try this code with Online Python Compiler
Run Code


Output:

At least one condition is true.

In this example, the condition x > 5 is true, so the OR operator returns true, and the message is printed.

Example 2: Combining Conditions with Integers

Here's how you might use the OR operator to check for multiple integer conditions.

  • Python

Python

a = 7

b = 3

if a < 5 or b > 2:

   print("At least one condition is true.")

else:

   print("Both conditions are false.")
You can also try this code with Online Python Compiler
Run Code

Output:

At least one condition is true.


In this case, b > 2 is true, so the OR operator returns true, and the message is printed.

Example 3: Using OR with Strings

The OR operator can also be used to check conditions with strings.

  • Python

Python

name = "Alice"

status = "Active"

if name == "Alice" or status == "Inactive":

   print("Name is Alice or status is Inactive.")

else:

   print("Neither condition is met.")
You can also try this code with Online Python Compiler
Run Code

Output

Name is Alice or status is Inactive.


Here, the condition name == "Alice" is true, so the OR operator returns true, and the corresponding message is printed.

Short-Circuit Evaluation

Python uses short-circuit evaluation with the OR operator. This means that if the first condition evaluates to true, Python does not check the remaining conditions. This can improve performance in certain cases.

Example 

  • Python

Python

def first_condition():

   print("First condition is evaluated.")

   return True

def second_condition():

   print("Second condition is evaluated.")

   return False

if first_condition() or second_condition():

   print("At least one condition is true.")

else:

   print("Both conditions are false.")
You can also try this code with Online Python Compiler
Run Code


Output

First condition is evaluated.
At least one condition is true.


In this example, since first_condition() returns True, second_condition() is not evaluated.

Frequently Asked Questions

What is the difference between | and or in Python?

The | operator is a bitwise OR, used for bit-level operations, while or is a logical OR used for boolean conditions.

How does the OR operator handle non-boolean values?

The OR operator evaluates non-boolean values by converting them to boolean values. Non-zero numbers and non-empty strings are considered True.

Can OR be used in list comprehensions?

Yes, you can use OR in list comprehensions to filter items based on multiple conditions.

What is the precedence of the OR operator?

The OR operator has lower precedence than the AND operator, so use parentheses to group conditions as needed.

Conclusion

The OR operator in Python is a versatile tool for combining multiple conditions in your code. By understanding how to use it effectively, you can create more flexible and readable conditional statements. Whether you're checking boolean values, integers, or strings, the OR operator helps you manage complex logic with ease. Remember to consider performance and proper grouping of conditions to write clean and efficient code.

Live masterclass