Free cookie consent management tool by TermsFeed The "or" keyword in Python | Pythontic.com

The "or" keyword in Python

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 1

Operand 2

Result

True

True

True

False

True

True

True

False

True

False

False

False

 

The Ven Diagram of the "or" operator:

Ven diagram of the logical operator - or

Example 1:

# 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:

Tuna

Item added successfully

Example 2:

# 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:

Buy order placed

 

 


Copyright 2025 © pythontic.com