The "or" keyword in Python

The "or" keyword in Python implements the logical OR operator. The "or" operator returns True, if at least one of the operands evaluate to True. Examples are given with output.

Keywords Updated 23 Dec 2017 1 min read

Overview

  • The "or" keyword implements the logical OR operation in Python.
  • In mathematics the OR operator returns True, if at least one of the operands evaluate to True.
  • In electronics the OR Gate implements the logical OR and the input/output are described by the truth table given below.
  • The "or" operator in Python returns the first operand if it is True else the second operand.

Truth table of the or Operator

Operand 1Operand 2Result
TrueTrueTrue
FalseTrueTrue
TrueFalseTrue
FalseFalseFalse

The Ven Diagram of the "or" operator

Ven diagram of the logical operator - or

Example 1

python
# Example python program using or operator

def addToBasket():

    print("Item added successfully")

def doNotAddToBasket():

    print("Can not add the item")

fish1        = None

fish2        = "Tuna"

validItem    = fish1 or fish2

if validItem:

    print(validItem)

    addToBasket()

else:

    donotAddToBasket()

Output

Output
Tuna

Item added successfully

Example 2

python
# Example python program using or operator

def buy():

    print("Buy order placed")

def sell():

    print("Sell order placed")

indicator1 = 10

indicator2 = 1.5

if indicator1 >= 10 or indicator2 < 1:

    buy()

else:

    sell()

Output

Output
Buy order placed