Python OR Logical Operator

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 – or keyword

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 – OR Logical Operation

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.

Example 1: Python Logical OR

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)
Run this program

Output

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

Example 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)
Run this program

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.