Skip to content

membership operator in python

Last Updated on July 31, 2023 by Oluwajuwon Alvina

Check out this article if you need to discover details about membership operator in python. Are you tired of spending endless time looking for relevant information online? That’s exactly why this article is what you need.

Additionally, you will find posts on the following subjects on Collegelearners.

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.
12345678910# 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.

123456789101112131415161718192021222324# 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=0d=0for i in list1: c+=1for i in list2: d+=1for i in range(0,c): for j in range(0,d): if(list1[i]==list2[j]): return 1return 0list1=[1,2,3,4,5] list2=[6,7,8,9] if(overlapping(list1,list2)): print("overlapping") else: print("not overlapping")

Output:

not overlapping

Course Curriculum

Python Programming Certification Training

Explore Curriculum

  • 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.
123456789101112131415# Python program to illustrate # not 'in' operator x = 24y = 20list = [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.
1234567# Python program to illustrate the use # of 'is' identity operator x = 6if (type(x) is int): print ("true") else: print ("false")

Output:

True

Let us take another example of the “in” operator.

Data Science Training

12345x = ["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.

1234567# Python program to illustrate the # use of 'is not' identity operator x = 7.2if (type(x) is not int): print ("true") else: print ("false")

Output:

True

Let us take another example of this operator.

12345x = ["apple", "banana"] print("pineapple" not in x) # returns True because a sequence with the value "pineapple" is not in the list

Join the conversation

Your email address will not be published. Required fields are marked *