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)

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)

Output

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

Summary

In this tutorial of Python Examples, we learned about or logical operator in Python, and how to use it, with the help of well detailed examples.