Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
Python is one of the most in-demand programming languages in the market today. Starting from amateurs all the way up to professionals, everyone uses Python thanks to its wide number of features as well as great versatility that it brings to the table. The not operator in Python is a part of the Membership Operators in Python. To understand its operation better, let us first take a look at the table of contents:
A Membership Operator in Python can be defined as being an operator that is used to validate the membership of a value. This operator is used to test memberships in variables such as strings, integers as well as tuples.
Membership Operators as a whole contain a number of different operators. Some of the most significant ones are as defined below :
In Operator: The in operator in Python is used to check if the value exists in a variable or not. When evaluated, if the operator finds a value then it returns true otherwise false. To understand this better, take a look at the example below.
# Python program to illustrate # Finding common member in list # using 'in' operator list1=[1,2,3,4,5] list2=[6,7,8,9] for item in list1: if item in list2: print("overlapping") else: print("not overlapping")
Output:
not overlapping
Now let us modify the above example and remove the in operator.
# Python program to illustrate # Finding common member in list # without using 'in' operator # Define a function() that takes two lists def overlapping(list1,list2): c=0 d=0 for i in list1: c+=1 for i in list2: d+=1 for i in range(0,c): for j in range(0,d): if(list1[i]==list2[j]): return 1 return 0 list1=[1,2,3,4,5] list2=[6,7,8,9] if(overlapping(list1,list2)): print("overlapping") else: print("not overlapping")
Output:
not overlapping
# Python program to illustrate # not 'in' operator x = 24 y = 20 list = [10, 20, 30, 40, 50 ]; if ( x not in list ): print ("x is NOT present in given list") else: print ("x is present in given list") if ( y in list ): print ("y is present in given list") else: print ("y is NOT present in given list")
Output:
x is NOT present in given list
y is present in given list
Other than membership operators, there exists another type of operators in Python which are known ad Identity Operators. In Python, identity operators are used to check if a particular value is of a certain class or type. In most cases, identity operators are used to define the type of data a certain variable contains. There are two main types of identity operators in Python.
# Python program to illustrate the use # of 'is' identity operator x = 6 if (type(x) is int): print ("true") else: print ("false")
Output:
True
Let us take another example of the “in” operator.
x = ["apple", "banana"] print("banana" is x) # returns True because a sequence with the value "banana" is in the list
True
The is not operator in Python is the exact opposite of the Is Operator. When evaluated, the operator returns false if the variables on either sides of the operator point to the same object and otherwise returns false. To understand this better, take a look at the example below.
# Python program to illustrate the # use of 'is not' identity operator x = 7.2 if (type(x) is not int): print ("true") else: print ("false")
Output:
True
Let us take another example of this operator.
x = ["apple", "banana"] print("pineapple" not in x) # returns True because a sequence with the value "pineapple" is not in the list
Output:
True
Both the identity operators as well as membership operators in Python can be used alternatively to make your program more efficient in the long run. Thus it is always advisable that you make use of both in your day to day programming and with this, we come to an end of this “Membership Operators in Python” article.
To get in-depth knowledge on Python along with its various applications, you can enroll now for the best Python course online training with 24/7 support and lifetime access.
Got a question for us? Mention them in the comments section of “Membership Operators in Python” and we will get back to you.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 26th October,2024 26th October SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 30th November,2024 30th November SAT&SUN (Weekend Batch) | View Details |
edureka.co