Thursday, December 26, 2024
Google search engine
HomeLanguagesPython not Keyword

Python not Keyword

Python not keyword is a logical operator which is usually used for figured out the negation or opposite boolean value of the operand. The Keyword ‘not’ is a unary type operator which means that it takes only one operand for the logical operation and returns the complementary of the boolean value of the operand. For example, if we will give false as an operand to the not keyword we get true as the value of returns. 

Syntax: not var

Practical Application

The possible practical application of not keyword are: 

  • This keyword is mostly used for altering the boolean value.
  • It is used with an if statement. In the if statement It is used for negating the condition
  • ‘not’ keyword is also used with in keyword. It is used with in keyword when we are searching for the specific value in the collection of data.

Example 1: Basic example of not operator with true variable.

Python3




# variable
a = True
print(not a)


Output:

False

Example 2: Basic example of not operator with variable.

Python3




# variable
a = False
print(not a)


Output:

True

Example 3: Example with Specific condition.

As basic property of the ‘not’ keyword is that it is used to invert the truth value of the operand. So we can see here that the result of every value is inverted from their true value. At #5 we can see the compare operation result would be false, so negation of it we get True value. Similar, we can see all results are inverted.

Python3




# Python code to demonstrate
# 'not' keyword
 
# Function showing working of not keyword
def geek_Func():
   
    # 1 Not with False boolean value
    geek_x = not False
    print('Negation  of False : ', geek_x)
 
    # 2 Not with true boolean value
    geek_y = not True
    print('Negation of True : ', geek_y)
 
    # 3 Not with result of and operation
    geek_and = not(True and False)
    print('Negation of result of And operation : ', geek_and)
 
    # 4 Not with result of or operation
    geek_or = not(True or False)
    print('Negation of result of or operation : ', geek_or)
 
    # 5 Not with result of compare operation
    geek_Com = not (5 > 7)
    print('Negation of result of And operation : ', geek_Com)
 
 
geek_Func()


Output: 

Negation  of False :  True
Negation of True :  False
Negation of result of And operation :  True
Negation of result of or operation :  False
Negation of result of And operation :  True

Example 4:  In this Code, we show the working of ‘not’ operator with a different value other than boolean, and see how it works. 

Python3




# Python code to demonstrate
# 'not' keyword
 
# Function showing working of not keyword
def geek_Func():
   
    # Not with String boolean value
    geek_Str = "geek"
    print('Negation  of String : ', not geek_Str)
 
    # Not with list boolean value
    geek_List = [1, 2, 3, 4]
    print('Negation of list : ', not geek_List)
 
    # Not with dictionary
    geek_Dict = {"geek": "sam", "collage": "Mit"}
    print('Negation of dictionary : ', not geek_Dict)
 
    # Not with Empty String
    geek_EDict = ""
    print('Negation of Empty String : ', not geek_EDict)
 
    # Not with Empty list
    geek_EList = []
    print('Negation of Empty List : ', not geek_EList)
 
    # Not with Empty dictionary
    geek_EStr = {}
    print('Negation of Empty Dictionary : ', not geek_EStr)
 
geek_Func()


Output: 

Negation  of String :  False
Negation of list :  False
Negation of dictionary :  False
Negation of Empty String :  True
Negation of Empty List :  True
Negation of Empty Dictionary :  True

In the above example we had seen that treating all the data types as operand with not keyword., ‘not’ treats true to all the data types who had value and false to those who were empty value.

Example 5: Example with the list

Python3




# Python code to demonstrate
# 'not' keyword
 
 
geek_list = [5, 10, 20, 59, 134, 83, 95]
 
# Function showing working of not keyword
def geek_Func():
   
    # Using not with if statement
    if not geek_list:
        print("Inputted list is Empty")
    else:
        for i in geek_list:
            if not(i % 5):
               
                # Using not with in statement
                if i not in (0, 10):
                    print("Multiple is not in range")
                else:
                    print(i)
            else:
                print("The number is not multiple of 5")
 
geek_Func()


Output: 

Multiple is not in range
10
MUltiple is not in range
The number is not multiple of 5
The number is not multiple of 5
The number is not multiple of 5
Multiple is not in range

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments