Python Programming (126 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (30 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (66 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Implement Membership Operators in Python

Published on Sep 25,2019 104 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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:

 

What is Membership Operators in Python?

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 in Python

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

 

  • Not In Operator: This operator is the exact opposite of the in operator. When evaluated this operator returns true if the value isn’t found and false if the value is found. Take a look at the example below to understand this better.
# 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

 

Identity Operators in Python

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.

  • Is Operator: When evaluated, the Is Operator in Python returns true if the variables on either side of the operator are pointing to the same variable and otherwise returns false. To understand this better, take a look at the example below.
# 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

Output:

True

 

Is Not Operator

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 here for live 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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.