Identity And Membership Operators In Python

Hello Pythonistas, here’s a quick reference to Identity and Membership operators in Python.

Identity

Whenever you create a variable in python it points out a location in memory because python is dynamically typed.

These operators check whether two variables or even values point to the same memory location or not.

OperatorFunctionExample
isReturns True if both point at the same location in memorya=10
b=10
print(a is b) #True
is notReturns False if both point at the same location in memorya=10
b=10
print(a is not b) #False

Membership

Have you ever joined a club, a football or basketball club?

If yes, how do you check whether you are a member?

Maybe by using a computer database or looking into the list of members.

Python uses membership operators to check if the value you entered is present in the sequence(say, list. It is a datatype) or not.

OperatorFunctionExample
inReturns True if the specified value is present in the given sequence.L = [1,2,3,10]
print(10 in L) #True
not inReturns False if the specified value is present in the given sequence.L = [1,2,3,10]
print(10 not in L) #False

Leave a Reply