The Difference Between Python is and == Operation: A Beginner Introduction

By | June 24, 2021

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.


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
>>> x == z
>>> x is y
>>> x is z
>>> print id(x)
>>> print id(y)
>>> print id(z)

Leave a Reply

Your email address will not be published. Required fields are marked *