Skip to content

Python Identity Operators

python identity operators

Python Identity operators compare two objects. Identity operator never checks the value of two objects but checks whether the two objects are same (same memory address) or not. All the identity operators and their application are shown below in the form of a table taking x and y as 2 and 3.

OperatorExampleDescriptionResult
isx is yReturns True if x and y are same object else FalseFalse
is notx is not yReturns True if x and y are different object else FalseTrue

How Python stores various variables in its memory ?

Before seeing Python Identity operator examples we need to know how Python stores various variables in its memory . For this reason we need to go to Python visualization tool . To go to Python visualization tool click here

In the Python visualization window we need to declare various variable and the window will shown us how Python stores that variable in its memory . First we will create x and y variable as value 1 and 2 respectively . After creating two variables we can see that visualizer have shown us how python stores the two variable in its memory –

From the above image we can see that Python stores x and y variable in different memory address . So , x and y are not same object they are different objects .

Note :

If the type of variable is int , float or str then Python will store different variable with same value in same memory . So , despite of different variable they will be treated as same object in Python . we can also check the below images for reference –

But for any other type of variable Python will store different variable with same value in different memory . So , despite of same value they will be treated as different object in Python . we can also check the below images for reference –

Python stores tuple and set variables same as like a list variable .

Python IS Operator

is operator returns True if the two variables are same object else False .

Example 1: Compare two integer using Python IS operator

x=10
y=10
print("x is y =",x is y)
x is y = True

In the above code as the x and y both store same integer value so Python treats them as same object . For this reason x is y returns True .

Example 2: Compare two list using Python IS operator

x=[1,2,3]
y=[1,2,3]
print("x is y =",x is y)
print("x[0] is y[0] =", x[0] is y[0])
x is y = False
x[0] is y[0] = True

In the above code as the type of x and y are list so despite of same value Python will treat them as different object . For this reason x is y returns False . But for x[0] and y[0] , both have same integer value of 1 ,so Python will treat them as same object . For this reason x[0] is y[0] returns True

Python IS NOT Operator

is not operator returns True if the two variables are different object else False .

Example 1: Compare two integer using Python IS NOT operator

x=10
y=10
print("x is not y =",x is not y)
x is not y = False

In the above code as the x and y both store same integer value so Python treats them as same object . For this reason x is not y returns False .

Example 2: Compare two list using Python IS NOT operator

x=[1,2,3]
y=[1,2,3]
print("x is not y =",x is not y)
print("x[0] is not y[0] =", x[0] is not y[0])
x is not y = True
x[0] is not y[0] = False

In the above code as the type of x and y are list so despite of same value Python will treat them as different object . For this reason x is not y returns True . But for x[0] and y[0] , both have same integer value of 1 ,so Python will treat them as same object . For this reason x[0] is not y[0] returns False

ID function in Python

id function in Python tells us what is the memory address of a variable . So , without going to Python Visualization tool with the help of id function we can check memory address of any two variables whether they are same or not .

Example 1: Compare two integer using ID function in Python

x=10
y=10
print(id(x))
print(id(y))
print("x is y =",x is y)
2807850560080
2807850560080
x is y = True
  • In the above code we have declared value of x and y as 10
  • Then we have printed memory address of both the variable with the help of id function
  • As memory address of both the variables are same(2807850560080) so , x is y returns True .

Example 2: Compare two list using ID function in Python

x=[1,2,3]
y=[1,2,3]

# printing memory address of both list
print(id(x))
print(id(y))
# As memory address of x and y are different
# So "x is y" returns False
print("x is y =",x is y)

# printing memory address of 1st element of both list
print(id(x[0]))
print(id(y[0]))
# As memory address of x[0] and y[0] are same
# So "x[0] is y[0]" returns True
print("x[0] is y[0] =", x[0] is y[0])
2807926878016
2807926876864
x is y = False
2807850559792
2807850559792
x[0] is y[0] = True

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply