Equality vs Identity vs Membership Operation
In python, we might have seen is
, in
, ==
in python.is
is used for identity comparison. in
is used for membership operations.==
is used for equality comparison.
Let’s learn more about this in this article.
Equality Comparison(==
)
Equality Comparison is done by ==
operator. It compares the value of two objects.
Sequences (instances of tuple, list, or range) can be compared only within each of their types. Equality comparison across these types results in inequality.
#listn1=[1,2,3]n2=[1,2,3]print (n1==n2)#Output:True#tuplet1=(1,2,3)t2=(1,2,3)print (t1==t2)#Output:True#range objectr1=range(5)r2=range(5)print (r1==r2)#Output:True#Comparing tuple and listprint (n1==t1)#Output:False
Mappings (instances of dict) compare equal if and only if they have equal (key, value) pairs.
d1={'a':1,'b':2}d2={'a':1,'b':2}print (d1==d2)#Output:True
Equality Comparison on string and int objects. They compare equal if their values are the same.
s="hello"s1="hello"s2="Hello"print (s==s1)#Output:Trueprint (s1==s2)#Output:Falsea=10b=10print (a==b)#Output:True
Identity Comparison (is)
Identity Comparison is done by using is
and is not
operator.
x is y →True if and only x and y are the same object.x is not y →True if x and y are not the same object
An Object’s identity is determined using the id()
function. This is the address of the object in memory.
Identical objects will always compare equal.x is y
implies x == y
Example 1:Testing is
operator in immutable objects like int, strings, tuple.
Immutable data types:
For immutable datatype, operations that compute new values may actually return a reference to any existing object with the same type and value.
Ex. a = 1; b = 1
, a
and b
may or may not refer to the same object with the value one, depending on the implementation
#inta=10b=10print (id(a))#Output:2047928384print (id(b))#Output:2047928384print (a is b)#Output:True#stringa="hello"b="hello"print (id(a))#Output:42785376print (id(b))#Output:42785376print (a is b)#Output:True#tuplea=(1,2,3)b=(1,2,3)print (id(a))print (id(b))print ( a is b)
Example 2:
Testing is
operator in mutable objects like a list.
Mutable data types:
c = []; d = []
c
and d
are guaranteed to refer to two different, unique, newly created empty lists.
a=[1,2,3]b=[1,2,3]print (id(a))#Output:13056008print (id(b))#Output:13190696print ( a is b)#Output:False
As per PEP 8
Comparisons to singletons like None
should always be done with is
or is not
, never the equality operators. Therefore, always be aware of writing if x when you really mean if x is not None.
When testing, if the variable or argument that defaults to None was set to some other value – you will need to determine whether the other value might have a type (such as a container). As that could be false in a boolean context!
Membership Operation(in)
Membership Operation is done by using in
and not in
operator.
x in s →Returns True if x is a member of s, False otherwise.x not in s →Returns True if x is not a member of s, False otherwise.
All built-in sequences, set types,dict, strings, byte types support membership operation
Strings:
x in y → True if an only x is a substring of y
Example 1:s1="hello"print ("h" in s1)#Output:Trueprint ("e" not in s1)#Output:False
2:
s1="hello"print (h in s1)#Output:NameError: name 'h' is not defined
3:
Empty strings are always considered as a substring of any other strings.
s1="hello"print ("" in s1)#Output:True
2. Sequences Types(List,tuple,range object)
#listn1=[1,2,3]print (1 in n1)#Output:Trueprint (2 not in n1)#Output:False#tuplet1=(1,2,3)print (1 in t1)#Output:Trueprint (2 not in t1)#Output:False#ranger1=range(5)print (1 in r1)#Output:Trueprint (2 not in r1)#Output:False
3.Set types (set,frozenset)
#sets1={1,2,3}print (1 in s1)#Output:Trueprint (2 not in s1)#Output:False#frozensets2=frozenset({1,2,3})print (1 in s2)#Output:Trueprint (2 not in s2)#Output:False
4.Dictionary
in
operator will check whether the dictionary has a given key.
1:
In the below example, 1 in d1
returns False, because in
will test only dictionary keys.
d1={'a':1,'b':2,'c':3}print ( 1 in d1)#Output:False
2:
If we mention d.values(), in
operator will test in dictionary values.
d={'a':1,'b':2,'c':3}print ( 1 in d.values())#Output:True
3:
If we mention d.items(),in
operator will test for the key, value pairs
d1={‘a’:1,’b’:2,’c’:3}print ( 1 in d1.items())#Output:Falseprint ((‘a’,1) in d1.items())#Output:True
Equality vs Identity vs Membership Operation
1.Syntax and functions used for these operations
2.Return type is Boolean for all these operations