Not in Python | With Uses and In-depth Explanation

Python being a user-friendly language allows us to use many English words. In Python, one such keyword is ‘not’. It is a very crucial keyword having many use-cases as various operators. For example, it can be used as logical operators to combine conditional statements. Not only as a logical operator, but also, not in Python can be used as ‘is not’ an identity and as ‘not in’ in membership operator.  

Identity operators are generally used to compare the objects, whether they are equal and in the same memory location or not. And to test whether a particular sequence is present in the object or not membership operator is used. 

Let us understand every use case of ‘not’ keyword in Python. 

Various forms of not in Python 

As a logical operator- 

Logical operators are used for combining conditional statements. There are three types of logical operators – ‘and,’ ‘or’ and ‘not.’  

‘not’ is a case–sensitive keyword and only returns Boolean values (True or False). It returns the reverse of the original result, i.e., if the first output was coming to be True, ‘not’ will convert it into False and vice versa. 

Let’s understand it with an example, and then we will discuss it in detail.  

Without ‘not’’- 

x=5 
y=2 
if (x>y): 
   print(x) 
else: 
  print(y) 

Output-
5 

Now let us use ‘not’ keyword   

x=5 
y=2 
#Here, x>y and it would have return True but due to ‘not’ it returned as False. 
if not(x>y): 
   print(x) 
else: 
   print(y) 

Output-
2 
not in python

Let us make a program to make a list empty and see where ‘not’ can be used. 

empty=False 
list1=[1,2,3,4,5,6,6,7,8,9,10] 
print("list before",list1) 
# not empty means 'TRUE' 
while (not empty): 
   # while the list1 is not empty 
   if list1 !=[]: 
        list1.pop()   
   else: 
       empty=True 
print("list after",list1) 

Output- 
list before [1, 2, 3, 4, 5, 6, 6, 7, 8, 9, 10] 
list after [] 
not in python

Not in Python as a Membership operator 

A very common use-case of not in any programming language is, which you may have used often, is ‘not in.’ We use it to whether a particular sequence is in an object or not. 

Suppose we want to check whether ‘peas’ is in the list of the fruits or not. 

fruits=["mango",
        "apple",
        "banana",
        "strawberry",
        "pineapple",
        "melon"] 
if 'peas' not in fruits: 
   print("Vegetable") 
else: 
   print("Fruit") 

Output- 
Vegetable 
not not in python as membership operator

To make it shorter and sweeter, We can use these operators to be used inside the print(). 

ages=[] 
# making a list of all ages allowed to vote 
for i in range(18,101): 
    ages.append(i) 
# If the given age is not allowed to vote print True 
print(17 not in ages) 

Output- 
True 

Program to List down marks in a list and add ‘Absent’ if name not in the dictionary 

dict1={"Ashwini":48,
       "khush":41,
       "Lamba":49,
       "Shera":30,
       "Arjun":47} 
list1=['Ashwini','Shalu','khush','Shabnam','Lamba','Manish'] 
list2=[] 
for i in list1: 
   if i not in dict1.keys(): 
       list2.append("Absent") 
   else: 
       list2.append(dict1[i]) 
print(list2) 

Output- 
[48, 'Absent', 41, 'Absent', 49, 'Absent'] 

Let’s create a very small login system using ‘not in’ keyword 

password_iscorrect=False 
login={'ashwini':'mandani'} 
while(not password_iscorrect): 
   username=input("Enter username ") 
   password=input("Enter password ") 
   if username in login.keys() and password not in login.values(): 
       password=input("Incorrect!! Enter correct password") 
        password_iscorrect=False 
   else: 
        print("Welcome ",username) 
        password_iscorrect=True 
Output- 
Enter username ashwini 
Enter password mandani 
Welcome ashwini  

As Identity Operator 

Identity operators are used to comparing objects. We have ‘is not’ operator in Python for this purpose.  

Program to divide list1 from list2, but if any element in list2=0 (Denominator cannot be equal to ‘0’). 

list1=[27,14,15,60,1,8] 
list2=[9,7,5,15,0,16] 
list3=[] 
for i in range(len(list1)): 
   #Denominator cannot be 0 
   if list2[i] is not 0: 
       list3.append(list1[i]/list2[i]) 
   else: 
       list3.append("Not Possible") 
print(list3) 

Output- 
[3.0, 2.0, 3.0, 4.0, 'Not Possible', 0.5] 
Identity operator

Another example can be if we want to sort elements on the basis of type, we can use is not in python. 

list1=[210.2,100,122,199.99,88,17.1,111,88,33.1,1011] 
# list containing integers 
int_list=[] 
# list containing float values 
float_list=[] 
for i in list1: 
   # if the data type of  
   if type(i) is not int: 
        float_list.append(i) 
   else: 
        int_list.append(i) 
print("float list",float_list) 
print("integer list",int_list) 

Output- 
float list [210.2, 199.99, 17.1, 33.1] 
integer list [100, 122, 88, 111, 88, 1011] 

Also Read:

Python Remove Duplicates From List
Python Print Without Newline
Learn Python the Hard Way Review PDF
How to Get a Data Science Internship With No Experience
Python Map

Conclusion- 

We have seen many examples of using not in Python and many different ways. In many places, we could have used ‘!=.’ instead of not a keyword. But the primary purpose of including these types of keywords in Python is to make it more user friendly by adding more English words. So, we must use these types of keywords to encourage the primary purpose behind the language.  

Leave a Reply