Hello Pythonistas, here’s a quick reference to Identity and Membership operators in Python.
Contents
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.
Operator | Function | Example |
---|---|---|
is | Returns True if both point at the same location in memory | a=10 |
is not | Returns False if both point at the same location in memory | a=10 |
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.
Operator | Function | Example |
---|---|---|
in | Returns True if the specified value is present in the given sequence. | L = [1,2,3,10] |
not in | Returns False if the specified value is present in the given sequence. | L = [1,2,3,10] |