How to use Identical and Membership Operators in Python 

There are two special operators in Python. Those are Identical & Membership. These operators look like English words. Here are the differences and how to use them.

Identical Operators

  • IS
  • IS NOT

Let me show how to use these two operators. Here is an example that exhibit how to use those.

Python logic for IS NOT

a = 'java'
b = 'php'

print ('Output is true because a and b values not matches', a is not b)

Here the identical operators give Boolean values. That means either True/False. When you run the above logic, you will get True since both a and b are not the same.

Python logic for IS NOT

Python logic for IS

a1 = 'java'
b1 = 'java'

print ('Output is True because a1 and b1 values matches', a1 is b1)

Here the output will be True since both a1 and b1 are the same. So we will get True.

Python logic for IS

Membership operators

Membership operators are two types. Those are in & not in. Here is a list of membership operators.

  • IN
  • NOT IN

Python logic for IN

c = int(input("Enter the value"))
print ("The value of c is: " + format(c))
lst1 = [12,3,5,6,9]
if c in lst1:
print('c value is available in the list', c in lst1)
else:
print('c value is not available in the list', c in lst1)

Here the output will be True since the entered value is in the list. Here is output.

Python logic for IN

Python logic for NOT IN

c = int(input("Enter the value"))
print ("The value of c is: " + format(c))
lst1 = [12,3,5,6,9]
if c in lst1:
print('c value is available in the list', c in lst1)
else:
print('c value is not available in the list', c in lst1)

Here the output will be False since the value your entered is not in the list. See the output.

Python logic for NOT IN

So you got to know the differences between identical and membership operators in python.

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.