0% found this document useful (0 votes)
13 views2 pages

Identity Operators

Identity Operators in Python are used to compare the memory addresses of two objects, consisting of 'is' and 'is not' operators. The 'is' operator returns True if both objects share the same memory address, while 'is not' returns True if they do not. Deep copies maintain the same address for objects, while shallow copies result in different addresses, affecting the outcome of these operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

Identity Operators

Identity Operators in Python are used to compare the memory addresses of two objects, consisting of 'is' and 'is not' operators. The 'is' operator returns True if both objects share the same memory address, while 'is not' returns True if they do not. Deep copies maintain the same address for objects, while shallow copies result in different addresses, affecting the outcome of these operators.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

===============================================================

7. Identity Operators

===============================================================
=>The purpose of Identity Operators is that "To Compare the Memory Address of Two
Objects".
=>In Python Programming, we have Two Types of Identity Operators. They are

1. is Operator
2. is not Operator
-----------------------------------------------------------------------------------
------------------------------------------------------------------------
1. is Operator
-----------------------------------------------------------------------------------
------------------------------------------------------------------------
Syntax: Object1 is Object2
=>The 'is' Operator returns True Provided the Memory Address of Object1 and Object2
are Same.
=>The 'is' Operator returns False Provided the Memory Address of Object1 and
Object2 are Different.
-----------------------------------------------------------------------------------
------------------------------------------------------------------------
2. is not Operator
-----------------------------------------------------------------------------------
------------------------------------------------------------------------
Syntax: Object1 is not Object2
=>The 'is not ' Operator returns True Provided the Memory Address of Object1 and
Object2 are Different.
=>The 'is not ' Operator returns False Provided the Memory Address of Object1 and
Object2 are Same.
-----------------------------------------------------------------------------------
------------------------------------------------------------------------
Most Imp
-----------------------------------------------------------------------------------
------------------------------------------------------------------------
NOTE: What are all the Objects are in DEEP COPY then Those Objects Contains Same
Address and 'is' Operator on those
Objects returns True and 'is not' returns False
NOTE: What are all the Objects are in SHALLOW COPY then Those Objects Contains
Different Address and 'is not'
Operator on those Objects returns True and 'is' returns False.
------------------------
Examples
------------------------
>>> lst1=[10,"RS",23.45]
>>> print(lst1,id(lst1))-----------------[10, 'RS', 23.45] 1479399606592
>>> lst2=lst1 # Deep Copy
>>> print(lst2,id(lst2))-----------------[10, 'RS', 23.45] 1479399606592
>>> lst1 is lst2---------------------------True
>>> lst1 is not lst2----------------------False
------------------------------------
>>> lst1=[10,"RS",23.45]
>>> print(lst1,id(lst1))---------------[10, 'RS', 23.45] 1479399618304
>>> lst2=lst1.copy()
>>> print(lst2,id(lst2))---------------[10, 'RS', 23.45] 1479399620480
>>> lst1 is lst2-------------------------False
>>> lst1 is not lst2--------------------True
-----------------------------------------------------------------------------------
------------------------------------------------------------------------

You might also like