not Operator in Python | Boolean Logic
Last Updated :
20 Dec, 2023
Python not keyword is a logical operator that is usually used to figure 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 give false as an operand to the not keyword we get true as the return value.
Syntax: not var
How to Use Not Operator in Python?
The not operator is very easy to use. You just need to use the ‘not’ keyword in front of the variable. Let’s understand it better with an example:
Example: Basic example of not operator with true variable.
Output:
False
As you can see from the above example, we just used not operator to change the true value to false.
Practical Applications
The possible practical applications of the ‘not’ keyword are:
- This keyword is mostly used for altering the boolean value.
- It is used with an if statement. It is used to negate the condition in the if statement,
- The ‘not’ keyword is also used with ‘in keyword‘. It is used with the ‘in’ keyword when we are searching for a specific value in the collection of data.
More Examples on Not Operator
Let’s look at some examples of not operator in Python codes, each example shows different use-cases of not operator.
Python not operator with Variable
Basic example of not operator with variable.
Output:
True
Using the “not” Boolean Operator in Python 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 by negation of it we get the True value. Similarly, we can see all results are inverted.
Python3
def geek_Func():
geek_x = not False
print ( 'Negation of False : ' , geek_x)
geek_y = not True
print ( 'Negation of True : ' , geek_y)
geek_and = not ( True and False )
print ( 'Negation of result of And operation : ' , geek_and)
geek_or = not ( True or False )
print ( 'Negation of result of or operation : ' , geek_or)
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
Using the Not Operator with different Value
In this Code, we show the working of the ‘not’ operator with a different value other than boolean, and see how it works.
Python3
def geek_Func():
geek_Str = "geek"
print ( 'Negation of String : ' , not geek_Str)
geek_List = [ 1 , 2 , 3 , 4 ]
print ( 'Negation of list : ' , not geek_List)
geek_Dict = { "geek" : "sam" , "collage" : "Mit" }
print ( 'Negation of dictionary : ' , not geek_Dict)
geek_EDict = ""
print ( 'Negation of Empty String : ' , not geek_EDict)
geek_EList = []
print ( 'Negation of Empty List : ' , not geek_EList)
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 saw that treating all the data types as operands with not keyword., ‘not’ treats true to all the data types who had value and false to those who were empty value.
Logical NOT operator with the list
Here in this example, we are using Not operator with the list:
Python3
geek_list = [ 5 , 10 , 20 , 59 , 134 , 83 , 95 ]
def geek_Func():
if not geek_list:
print ( "Inputted list is Empty" )
else :
for i in geek_list:
if not (i % 5 ):
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
We have covered the meaning, syntax, and uses of the not operator in Python. This might have given you the complete picture of what is not in Python. You can look at the above examples or experiment on your device about not operator. It is a very basic yet useful operator in Python.
Read More on Python Operator
Similar Reads
Please Login to comment...