Python OR Operator - Examples


Python OR

To perform logical OR operation in Python, you can use or keyword.

In this tutorial, we shall learn how Python or logical operator works with boolean values and integer operands, with the help of example programs.


Syntax of OR Operator

The syntax to use or operator is given below.

operand1 or operand2

or logical operator accepts two operands.

or operator returns true if any of the operands is true.


Truth Table

Following is the truth table with the Return values for possible combinations of the operands to OR operator.

Operand1Operand2Return Value
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

To summarize, if any of the operands is True, then OR operator returns True.


Examples

1. Python Logical OR with boolean values

Following example, demonstrates the usage of or keyword to do logical or operation. In this example, we take two variables and their possible logical value combinations.

Python Program

#True or True
a = True
b = True

c = a or b
print(a,'or',b,'is:',c)

#True or False
a = True
b = False

c = a or b
print(a,'or',b,'is:',c)

#False or True
a = False
b = True

c = a or b
print(a,'or',b,'is:',c)

#False or False
a = False
b = False

c = a or b
print(a,'or',b,'is:',c)

Explanation of the Code:

  1. First Case (True or True): The result is True because both operands are True.
  2. Second Case (True or False): The result is True because one of the operands is True.
  3. Third Case (False or True): The result is True because one of the operands is True.
  4. Fourth Case (False or False): The result is False because both operands are False.

Output

True or True is: True
True or False is: True
False or True is: True
False or False is: False

2. OR Operator with non-boolean operands

When using in boolean expressions, you can also use non-zero number for True and zero for False.

Python Program

#True or True
a = 5
b = -5
c = a or b
print(a,'or',b,'is:',c)

#True or False
a = 4
b = 0
c = a or b
print(a,'or',b,'is:',c)

#False or True
a = 0
b = 6
c = a or b
print(a,'or',b,'is:',c)

#False or False
a = 0
b = 0
c = a or b
print(a,'or',b,'is:',c)

Explanation of the Code:

  1. First Case (5 or -5): The result is 5 because both operands are non-zero, and in Python, any non-zero number is considered True.
  2. Second Case (4 or 0): The result is 4 because the first operand is non-zero, so it's considered True, and Python returns the first non-zero value.
  3. Third Case (0 or 6): The result is 6 because the second operand is non-zero, so Python returns the second operand as the result.
  4. Fourth Case (0 or 0): The result is 0 because both operands are zero, which are treated as False.

Output

5 or -5 is: 5
4 or 0 is: 4
0 or 6 is: 6
0 or 0 is: 0

3. OR Operator with Strings

The or operator can also be used with strings. If one string is empty (which evaluates to False), Python will return the non-empty string (which evaluates to True).

Python Program

#Empty string or non-empty string
str1 = ''
str2 = 'Hello'
result = str1 or str2
print('Result of empty string or non-empty string is:', result)

#Non-empty string or empty string
str1 = 'Python'
str2 = ''
result = str1 or str2
print('Result of non-empty string or empty string is:', result)

Explanation of the Code:

  1. First Case (empty string or non-empty string): Since the first string is empty (evaluates to False), Python returns the second string as the result.
  2. Second Case (non-empty string or empty string): Since the first string is non-empty (evaluates to True), Python returns the first string as the result.

Output

Result of empty string or non-empty string is: Hello
Result of non-empty string or empty string is: Python

Summary

In this tutorial, we learned about the or logical operator in Python and its usage with boolean values, integer operands, and strings. We explored different examples and edge cases to better understand its functionality.




Python Libraries