Equality vs Identity vs Membership Operation in Python

'==’ vs ‘is vs ‘in operator in Python

Photo by Rodolfo Clix from Pexels

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 about this in detail 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.

#list
n1=[1,2,3]
n2=[1,2,3]
print (n1==n2)#Output:True

#tuple
t1=(1,2,3)
t2=(1,2,3)
print (t1==t2)#Output:True

#range object
r1=range(5)
r2=range(5)
print (r1==r2)#Output:True

#Comparing tuple and list
print (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:True
print (s1==s2)#Output:False

a=10
b=10
print (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

#int
a=10
b=10
print (id(a))#Output:2047928384
print (id(b))#Output:2047928384
print (a is b)#Output:True

#string
a="hello"
b="hello"
print (id(a))#Output:42785376
print (id(b))#Output:42785376
print (a is b)#Output:True

#tuple
a=(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:13056008
print (id(b))#Output:13190696
print ( 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.

Also, beware of writing if x when you really mean if x is not None — e.g. when testing whether a variable or argument that defaults to None was set to some other value. The other value might have a type (such as a container) 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

  1. Strings:
    x in y → True if an only x is a substring of y

Example 1:

s1="hello"
print ("h" in s1)#Output:True
print ("e" not in s1)#Output:False

Example 2:

s1="hello"
print (h in s1)
#Output:NameError: name 'h' is not defined

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

#list
n1=[1,2,3]
print (1 in n1)#Output:True
print (2 not in n1)#Output:False

#tuple
t1=(1,2,3)
print (1 in t1)#Output:True
print (2 not in t1)#Output:False

#range
r1=range(5)
print (1 in r1)#Output:True
print (2 not in r1)#Output:False

3.Set types (set,frozenset)

#set
s1={1,2,3}
print (1 in s1)#Output:True
print (2 not in s1)#Output:False

#frozenset
s2=frozenset({1,2,3})
print (1 in s2)#Output:True
print (2 not in s2)#Output:False

4.Dictionary

in operator will check whether the dictionary has a given key.

Example 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

Example 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

Example 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:False
print (('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


Resources(Python Documentation)

Operators

Comparison Operator

Membership Operation

comparisons

PEP 8

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s