=====================================================
Identity Operators--Python Command / IDLE Shell Basis
=====================================================
=>The purpose of Identity Operators is that "To Compare Memory Address of Two
Objects".
=>In Python Programming, we have Two Types of Identity Operators. They are
1. is
2. is not
-----------------------------------------------------------------------------------
-----------------------------------------------------
1. is
-----------------------------------------------------------------------------------
-----------------------------------------------------
=>Syntax: Object1 is Object2
=>"is" Operator returns True Provided Both the objects Contains SAME Address.
=>"is" Operator returns False Provided Both the objects Contains Different Address.
-----------------------------------------------------------------------------------
-----------------------------------------------------
2. is not
-----------------------------------------------------------------------------------
-----------------------------------------------------
Syntax: Object1 is not Object2
=>"is not " Operator returns True Provided Both the objects Contains Different
Address.
=>"is not " Operator returns False Provided Both the objects Contains Same Address.
-----------------------------------------------------------------------------------
-----------------------------------------------------
Examples:
-----------------------------------------------------------------------------------
-----------------------------------------------------
>>> a=None
>>> b=None
>>> print(a,type(a),id(a))---------None <class 'NoneType'> 140704386324472
>>> print(b,type(b),id(b))--------None <class 'NoneType'> 140704386324472
>>> a is b---------True
>>> a is not b---------False
-----------------------------------------------------------------------------------
-------------------------------------------------
>>> d1={10:"Apple",20:"Mango",30:"Kiwi"}
>>> d2={10:"Apple",20:"Mango",30:"Kiwi"}
>>> print(d1,type(d1),id(d1))-----{10: 'Apple', 20: 'Mango', 30: 'Kiwi'} <class
'dict'> 2864804287360
>>> print(d2,type(d2),id(d2))-----{10: 'Apple', 20: 'Mango', 30: 'Kiwi'} <class
'dict'> 2864804287488
>>> d1 is d2--------False
>>> d1 is not d2--True
-----------------------------------------------------------------------------------
-------------------------------------------------
>>> s1={10,20,30,40}
>>> s2={10,20,30,40}
>>> print(s1,type(s1),id(s1))---------{40, 10, 20, 30} <class 'set'> 2864805025088
>>> print(s2,type(s2),id(s2))---------{40, 10, 20, 30} <class 'set'> 2864805024640
>>> s1 is s2-------------------False
>>> s1 is not s2-------------True
>>> fs1=frozenset(s1)
>>> fs2=frozenset(s1)
>>> print(fs1,type(fs1),id(fs1))--------frozenset({40, 10, 20, 30}) <class
'frozenset'> 2864805026208
>>> print(fs2,type(fs2),id(fs2))-------frozenset({40, 10, 20, 30}) <class
'frozenset'> 2864805026656
>>> fs1 is fs2-------------------------False
>>> fs1 is not fs2-------------------True
-----------------------------------------------------------------------------------
-------------------------------------------------
>>> l1=[10,"Rossum",22.22]
>>> l2=[10,"Rossum",22.22]
>>> print(l1,type(l1),id(l1))----------[10, 'Rossum', 22.22] <class 'list'>
2864804317696
>>> print(l2,type(l2),id(l2))---------[10, 'Rossum', 22.22] <class 'list'>
2864804318400
>>> l1 is l2------------------False
>>> l1 is not l2-----------True
>>> t1=(10,20,30,40)
>>> t2=(10,20,30,40)
>>> print(t1,type(t1),id(t1))------------(10, 20, 30, 40) <class 'tuple'>
2864804748304
>>> print(t2,type(t2),id(t2))------------(10, 20, 30, 40) <class 'tuple'>
2864804752304
>>> t1 is t2--------------------------------False
>>> t1 is not t2--------------------------True
-----------------------------------------------------------------------------------
-------------------------------------------------
>>> r1=range(10,20)
>>> r2=range(10,20)
>>> print(r1,type(r1),id(r1))--------range(10, 20) <class 'range'> 2864804444656
>>> print(r2,type(r2),id(r2))--------range(10, 20) <class 'range'> 2864804444224
>>> r1 is r2----------------------False
>>> r1 is not r2---------------True
>>> ba1=bytearray([10,20,30,40])
>>> ba2=bytearray([10,20,30,40])
>>> print(ba1,type(ba1),id(ba1))---bytearray(b'\n\x14\x1e(') <class 'bytearray'>
2864808582512
>>> print(ba2,type(ba2),id(ba2))---bytearray(b'\n\x14\x1e(') <class 'bytearray'>
2864808582896
>>> ba1 is ba2--------False
>>> ba1 is not ba2---True
>>> ba1=bytes([10,20,30,40])
>>> ba2=bytes([10,20,30,40])
>>> print(ba1,type(ba1),id(ba1))----b'\n\x14\x1e(' <class 'bytes'> 2864804444320
>>> print(ba2,type(ba2),id(ba2))---b'\n\x14\x1e(' <class 'bytes'> 2864804442592
>>> ba1 is ba2-----False
>>> ba1 is not ba2----True
---------------------
>>> s1="PYTHON"
>>> s2="PYTHON"
>>> print(s1,type(s1),id(s1))---PYTHON <class 'str'> 2864808583024
>>> print(s2,type(s2),id(s2))---PYTHON <class 'str'> 2864808583024
>>> s1 is s2-----True
>>> s1 is not s2---False
----------------------------
>>> s1="JAVA"
>>> s2="JAVa"
>>> print(s1,type(s1),id(s1))--------JAVA <class 'str'> 2864808582000
>>> print(s2,type(s2),id(s2))-------JAVa <class 'str'> 2864808582512
>>> s1 is s2------------False
>>> s1 is not s2------True
-----------------------------------------------------------------------------------
---------------------------------------------------
>>> a=2+3j
>>> b=2+3j
>>> print(a,type(a),id(a))--------(2+3j) <class 'complex'> 2864803995536
>>> print(b,type(b),id(b))-------(2+3j) <class 'complex'> 2864803999280
>>> a is b-----------False
>>> a is not b-----True
>>> a=True
>>> b=True
>>> print(a,type(a),id(a))---------True <class 'bool'> 140704386272104
>>> print(b,type(b),id(b))--------True <class 'bool'> 140704386272104
>>> a is b--------True
>>> a is not b---False
>>> c=False
>>> print(c,type(c),id(c))-----False <class 'bool'> 140704386272136
>>> a is c-------False
>>> a is not c-------True
>>> a=1.2
>>> b=1.2
>>> print(a,type(a),id(a))---------1.2 <class 'float'> 2864803995984
>>> print(b,type(b),id(b))--------1.2 <class 'float'> 2864803999280
>>> a is b--------------------False
>>> a is not b--------------True
----------------------------
>>> a=300
>>> b=300
>>> print(a,type(a),id(a))-----300 <class 'int'> 2864803995600
>>> print(b,type(b),id(b))----300 <class 'int'> 2864803999184
>>> a is b-----------False
>>> a is not b------True
>>> a=10
>>> b=10
>>> print(a,type(a),id(a))--------10 <class 'int'> 2864802955792
>>> print(b,type(b),id(b))-------10 <class 'int'> 2864802955792
>>> a is b---------True
>>> a is not b------False
>>> a=256
>>> b=256
>>> print(a,type(a),id(a))----256 <class 'int'> 2864802963664
>>> print(b,type(b),id(b))----256 <class 'int'> 2864802963664
>>> a is b--------True
>>> a is not b---False
>>> a=257
>>> b=257
>>> print(a,type(a),id(a))----257 <class 'int'> 2864803995600
>>> print(b,type(b),id(b))---257 <class 'int'> 2864803999248
>>> a is b-------False
>>> a is not b----True
>>> a=0
>>> b=0
>>> print(a,type(a),id(a))------0 <class 'int'> 2864802955472
>>> print(b,type(b),id(b))-----0 <class 'int'> 2864802955472
>>> a is b------------True
>>> a is not b-------False
---------------------------------------
>>> a=-1
>>> b=-1
>>> print(a,type(a),id(a))---------- -1 <class 'int'> 2864802955440
>>> print(b,type(b),id(b))--------- -1 <class 'int'> 2864802955440
>>> a is b--------True
>>> a is not b---False
>>> a=-5
>>> b=-5
>>> print(a,type(a),id(a))------------ -5 <class 'int'> 2864802955312
>>> print(b,type(b),id(b))------------ -5 <class 'int'> 2864802955312
>>> a is b--------------True
>>> a is not b---------False
>>> a=-6
>>> b=-6
>>> print(a,type(a),id(a))-------- -6 <class 'int'> 2864803999248
>>> print(b,type(b),id(b))------ -6 <class 'int'> 2864803995600
>>> a is b-----------------False
>>> a is not b----------True
-----------------------------------------------------------------------------------
----------
MOST IMP
-----------------------------------------------------------------------------------
----------
>>> a,b=400,400 # Multi Line Assigment
>>> print(a,type(a),id(a))-----400 <class 'int'> 2864803995728
>>> print(b,type(b),id(b))----400 <class 'int'> 2864803995728
>>> a is b-----True
>>> a is not b---False
>>> a,b=1.2,1.2 # # Multi Line Assigment
>>> print(a,type(a),id(a))---------1.2 <class 'float'> 2864803999120
>>> print(b,type(b),id(b))---------1.2 <class 'float'> 2864803999120
>>> a is b---------True
>>> a is not b----False
>>> a,b=2+3j,2+3j
>>> print(a,type(a),id(a))-------(2+3j) <class 'complex'> 2864803999344
>>> print(b,type(b),id(b))------(2+3j) <class 'complex'> 2864803999344
>>> a is b--------------------True
>>> a is not b--------------False
-----------------------------------------------------------------------------------
----------
MOST MOST IMP
-----------------------------------------------------------------------------------
----------
>>> l1,l2=[10,20,30],[10,20,30]
>>> print(l1,type(l1),id(l1))---------[10, 20, 30] <class 'list'> 2864804318400
>>> print(l2,type(l2),id(l2))--------[10, 20, 30] <class 'list'> 2864808577600
>>> l1 is l2---------------False
>>> l1 is not l2--------True
>>> d1,d2={10:"Python",20:"Java"},{10:"Python",20:"Java"}
>>> print(d1,type(d1),id(d1))---{10: 'Python', 20: 'Java'} <class 'dict'>
2864804318976
>>> print(d2,type(d2),id(d2))---{10: 'Python', 20: 'Java'} <class 'dict'>
2864804287872
>>> d1 is d2----------False
>>> d1 is not d2-----True
-----------------------------------------------------------------------------------
----------------------------------------------------
NOTE:
----------
=>What are all the objects Participates in DEEP Copy then Those Objects Contains
Same Address and "is" operator Returns True and "is not" operator Returns False.
=>What are all the objects Participates in SHALLOW DEEP Copy then Those Objects
Contains Different Address and "is" operator Returns False and "is not" operator
Returns True.
-----------------------------------------------------------------------------------
----------------------------------------------------