# What is "is" in Python?

Updated: Sep 23, 2019

The operators is and is not test for object identity. x is y is true if and only if x and y are the same object (object location in memory is same). Object identity is determined using the id() function. x is not y yields the inverse truth value.

It’s easy to understand if I compare this operator with equality operator.

is and is not compares the object reference. Check for identity.

== and != compares the object value. Check for equality.

For example, if you consider integer objects (excluding integers from -5 to 256),

`>>> A=9999 `

`>>> B=9999 `

`>>> A == B, A is B`

`(True, False) `

`>>> A, B`

`(9999, 9999) `

`>>> id(A), id(B)`

`(4452685328, 4452686992)`

Python stores integer objects as single object between range -5 to 256 so the identity is same.

`>>> A=99`

`>>> B=99 `

`>>> A == B, A is B`

`(True, True) `

`>>> A, B`

`(99, 99) `

`>>> id(A), id(B)`

`(4404392064, 4404392064)`

Lets see behavior of other immutable objects like int & float, string, tuples and boolean:

`>>> 1 == 1.0, 1 is 1.0`

`(True, False) `

`>>> 1 == 1, 1 is 1`

`(True, True) `

`>>> 'A' == 'A', 'A' is 'A'`

`(True, True) `

`>>> (1,2) == (1,2), (1,2) is (1,2)`

`(True, True) `

`>>> True == True, True is True`

`(True, True)`

What about mutable objects - list, set, dict? Behavior is totally different.

`>>> [1,2] == [1,2], [1,2] is [1,2]`

`(True, False) `

`>>> {1,2} == {1,2}, {1,2} is {1,2}`

`(True, False) `

`>>> {'k1':1} == {'k1':1}, {'k1':1} is {'k1':1}`

`(True, False)`

is operator is used only when you want to compare the object identity, for regular comparison equality operator is used.

## Commentaires