Last modified: Sep 10, 2026
How to Type OR in Python
In Python, the OR operator is used to combine multiple conditions. It returns True if at least one condition is true. This article explains how to type and use OR in Python effectively.
Python provides two ways to write the OR operator:
or(lowercase keyword)|(pipe symbol for bitwise OR)
The choice depends on your use case. For logical comparisons, use or. For bitwise operations, use |.
Using the OR Keyword
The or keyword is the standard way to perform logical OR operations in Python. It evaluates to True if either operand is true.
# Example: Using 'or' for logical comparison
a = 5
b = 10
result = a > 10 or b > 5
print(result) # Output: True
True
In this example, even though the first condition (a > 10) is false, the second condition (b > 5) is true. So the overall result is True.
OR with Strings
You can also use or with strings. In Python, empty strings are considered False, while non-empty strings are True.
# Example: Using 'or' with strings
text = ""
default = "Hello World"
result = text or default
print(result)
Hello World
Since text is an empty string (falsy), the expression returns the value of default.
Using the Pipe Symbol (|)
The pipe symbol (|) performs bitwise OR operations. It compares the binary representations of integers.
# Example: Using '|' for bitwise OR
x = 5 # Binary: 0101
y = 3 # Binary: 0011
result = x | y
print(result) # Output: 7 (Binary: 0111)
7
Bitwise OR compares each bit. If either bit is 1, the result bit is 1.
Short-Circuit Evaluation
Python uses short-circuit evaluation with or. It stops evaluating as soon as it finds a true value.
# Example: Short-circuit evaluation
def check_true():
print("Checking...")
return True
def check_false():
print("Not checking...")
return False
result = check_false() or check_true()
print(result)
Not checking...
Checking...
True
Since check_false() returns False, Python evaluates check_true(). If the first function returned True, the second would not be called.
Common Use Cases
Conditional Statements
Use or in if statements to check multiple conditions.
# Example: Using 'or' in if statement
day = "Saturday"
if day == "Saturday" or day == "Sunday":
print("It's the weekend!")
else:
print("It's a weekday.")
It's the weekend!
Default Values
Use or to assign default values when variables are empty or undefined.
# Example: Default values with 'or'
name = ""
greeting = "Hello, " + (name or "Guest")
print(greeting)
Hello, Guest
This is useful when dealing with user input or optional parameters.
Operator Precedence
Understanding precedence helps avoid bugs. In Python, or has lower precedence than and.
# Example: Operator precedence
a = True
b = False
c = True
result = a or b and c
print(result) # Output: True
True
This evaluates as a or (b and c). Use parentheses for clarity.
Chaining Multiple OR Conditions
You can chain multiple or operators for complex conditions.
# Example: Chaining OR conditions
status = "pending"
if status == "approved" or status == "pending" or status == "review":
print("Processing request...")
else:
print("Invalid status.")
Processing request...
Best Practices
- Use
orfor logical comparisons, not| - Use parentheses to make complex expressions clearer
- Remember that
orreturns the actual value, not justTrueorFalse - Consider performance impact of short-circuit evaluation
Related Topics
For more information on logical operations in Python, check out our guides on Python Booleans and Boolean Combinations.
Conclusion
The or operator is essential for writing flexible Python code. Whether you're checking conditions or setting defaults, understanding how to use OR properly will make your programs more robust. Practice with different scenarios to master this fundamental concept.