NOT

What Is the NOT Operator?

  • The NOT operator not in Python is used to reverse the boolean value of a statement.

How to Use It:

  • The basic way to use the NOT operator is:

    python
    result = not operand
    
    • operand is the condition you want to reverse.
    • result will be True if operand is False, and False if operand is True.

Truth Table - NOT Operator:

  • Here’s how the NOT operator works:

     
    Operand  Return Value
    True     False
    False    True
    

Examples and Exercises:

  1. Using NOT with Boolean Values:

    python
    # not True
    a = True
    c = not a
    print('not', a, 'is:', c)  # Output: not True is: False
    
    # not False
    a = False
    c = not a
    print('not', a, 'is:', c)  # Output: not False is: True
    
  2. Using NOT with Non-Boolean Values:

    • In Python, any non-zero value is considered True and zero is considered False.
    python
    # not True
    a = 5
    c = not a
    print('not', a, 'is:', c)  # Output: not 5 is: False
    
    # not False
    a = 0
    c = not a
    print('not', a, 'is:', c)  # Output: not 0 is: True
    

Summary:

  • The NOT operator not in Python is useful for reversing the boolean value of a condition. It helps in making decisions in your code. Try the examples to see how it works!