Python Identity Vs. Membership Operators

You need identity operator to compare the strings. Similarly, you need membership operators to check if a value is present in another list /or not.

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

IN THIS PAGE

  1. Identity Operator
    1. Python logic for IS NOT
    2. Python logic for IS
  2. Membership operator
    1. Python logic for IN
    2. Python logic for NOT IN

Identity Operator

The are two identity operators in python.

  • IS
  • IS NOT

Python logic for IS NOT

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

Identical operators result in Boolean values – True/False. After you run the above logic, you will get True since a and b are distinct.

Python logic for IS NOT
Identical operators

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
Examples for identity operators

Membership operator

There are two membership operators in python. Those are in & not in.

  • 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)

The output is True since the value you enter is in the list. Here is the output.

Python logic for IN
Membership operators

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)

The output is False since the value you enter is not in the list. Here’s the output.

Python logic for NOT IN
Example for membership operators

I am sure the examples above provided 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.