What is membership and identity operators in python?
Answers
Answer:
Explanation:
As the name explains the membership operators are used to validate the membership of a value. These operators are used to find out whether a value is a member of a sequence such as string or list membership operators are of two types: (1) in and (2) not in.
Identity operators are used to verify if two variables point to the same memory location or not. Identity operators are of two types: (1) is and (2) is not.
in operator
The ‘in’ operator is used to check if a value exists in a sequence or not. If it exists in the sequence then it will return a true else it will return a false. Following are two examples that show how this operator can be used. The first example tries to check if a value exists in a list and the second example tries to find out if a value exists in a string.
Example 1.
lst1 = ['Ajay', 'Bobby','Ashok', 'Vijay', 'Anil', 'Rahul','Alex', 'Christopher']
if 'Ajay' in lst1: print('Name Ajay exists in lst1')
Example2
if 'j' in 'Ajay': print("letter 'j' exists in name 'Ajay'")
else: print ("letter 'j' does not exists in name 'Ajay'")
if 'm' in 'Ajay': print("letter 'm' exists in name 'Ajay'")
else: print ("letter 'm' does not exists in name 'Ajay'")
not in operator
The ‘not in’ operator is the opposite of ‘in’ operator. So, if a value does not exists in the sequence then it will return a true else it will return a false. Following are two examples that show how this operator can be used. The first example shows how this operator can be used with a list and the second example deals with string.
Example 1
lst1 = ['Ajay', 'Bobby','Ashok', 'Vijay', 'Anil', 'Rahul','Alex', 'Christopher']
if 'Raghav' not in lst1: print ('Name Raghav does not exists in lst1')
Example 2
if 'j' not in 'Ajay': print ("letter 'j' does not exists in name 'Ajay'")
else: print ("letter 'j' exists in name 'Ajay'")
if 'm' not in 'Ajay': print("letter 'm' does not exists in name 'Ajay'")
else: print ("letter 'm' exists in name 'Ajay'")
is operator
’is’ operator returns true if both the operand point to same memory location. The following example deals with three values. The variable having same value point to same memory location; So, for them this operator returns a ‘true’.
a = 'London'
b = 'London'
c = 'Paris'
if a is b: print ('a is b')
else: print ('a is not b')
if a is c: print('a is c')
else: print ('a is not c')
if b is c: print('b is c')
else: print ('b is not c')
Download the code Run the code
is not operator
’is not’ operator returns true if both the operand point to different memory location. It is the opposite of ‘is’ operator. The following example deals with three values. The variable having same value point to same memory location; So, for them this operator returns a ‘false’.
a = 'London'
b = 'London'
c = 'Paris'
if a is not b: print ('a is not b')
else: print ('a not b')
if a is not c: print ('a is not c')
else: print ('a is c')
if b is not c: print ('b is not c')
else: print ('b is c')