The not keyword in Python

Overview:

  • The not keyword implements the negate operator or logical complement in Python.
  • The not is a unary operator. It takes only operand and returns the complement of it.

Truth table of the Python not operator:

Operand

Result

True

False

False

True

Ven diagram of the Python not operator:

Venn diagram of the logical operator - not

Example 1:

# Example python program using not operator

 

switch = False

for i in range(1,10):

    # toggle using negate every time

    switch = not switch

    print(switch)

 

Output 1:

True

False

True

False

True

False

True

False

True

 

 

Example 2:

# Example python program using not operator

 

def CloseOrderBook():

    print("Order book closed successfully")

 

marketOpen = False

 

if not marketOpen:

    CloseOrderBook()

    print("Market closed")

   

 

 

Output 2:

Order book closed successfully

Market closed

 

 

 


Copyright 2024 © pythontic.com