Skip to content
Related Articles

Related Articles

Python Membership and Identity Operators

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 23 Jun, 2022

In this article, we are going to learn Python Membership and Identity Operators.

Membership Operators

Python offers two membership operators to check or validate the membership of a value. It tests for membership in a sequence, such as strings, lists, or tuples. 

in operator: The ‘in’ operator is used to check if a character/ substring/ element exists in a sequence or not. Evaluate to True if it finds the specified element in a sequence otherwise False. For example,

'G' in 'GeeksforGeeks'   # Checking 'G' in String
True
'g' in 'GeeksforGeeks'   #Checking 'g' in string since Python is case-sensitive,returns False
False
'Geeks' in ['Geeks', 'For','Geeks']   #Checking 'Geeks' in list of strings
True
10 in [10000,1000,100,10]        #Checking 10 in list of integers
True
dict1={1:'Geeks',2:'For',3:'Geeks'}     # Checking 3 in keys of dictionary
3 in dict1
True

Python3




# 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
not overlapping
not overlapping
not overlapping
not overlapping

The same example without using in operator:

Python3




# 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- Evaluates to true if it does not finds a variable in the specified sequence and false otherwise.

Python3




# 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

Identity operators are used to comparing the objects if both the objects are actually of the same data type and share the same memory location.
There are different identity operators such as 

‘is’ operator – Evaluates to True if the variables on either side of the operator point to the same object and false otherwise.

Python3




# Python program to illustrate the use
# of 'is' identity operator
x = 5
y = 5
print(x is y)
id(x)
id(y)


True
140704586672032
140704586672032

Here in the given example, both the variables x and y have value 5 assigned to it and both share the same memory location, which is why returns True.

‘is not’ operator – Evaluates to false if the variables on either side of the operator point to a different object and true otherwise.

Python3




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


Output:
False
True


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!