In python, we can use is or == operation to compare two objects. In this tutorial, we will introduce the difference between them.
Generally, a python object contains three properties: data type, value and memory address.
For example:
x = [3, 1]
x is a python list (data type), its value is 3, the memory address may be 140418483448744.
We can use id() function to get python object memory address.
print(id(x))
The difference between python is and ==
Python is: It will compare the memory address of two objects, which means we will compare the value of id().
Python ==: it will compare the value of two objects.
Here is an example:
>>> x = y = [4,5,6] >>> z = [4,5,6] >>> x == y True >>> x == z True >>> x is y True >>> x is z False >>> >>> print id(x) 3075326572 >>> print id(y) 3075326572 >>> print id(z) 3075328140