The not keyword in Python

The not keyword in python implements the negation or logical complement. If the operand is True it returns False and returns True otherwise.Example and Output is Provided.

Keywords Updated 23 Dec 2017 1 min read

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

OperandResult
TrueFalse
FalseTrue

Ven diagram of the Python not operator

Venn diagram of the logical operator - not

Example 1

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

Output
True

False

True

False

True

False

True

False

True

Example 2

python
# Example python program using not operator

def CloseOrderBook():

    print("Order book closed successfully")

marketOpen = False

if not marketOpen:

    CloseOrderBook()

    print("Market closed")

Output 2

Output
Order book closed successfully

Market closed