Udit Vashisht
Author: Udit Vashisht


Difference between "==" and "is" in Python- Quick Tip

  • 4 minutes read
  • 79 Views
Difference between "==" and "is" in Python- Quick Tip

    Table of Contents

What is the difference between “==” and “is” variable in Python?

If you are new to python you must have seen programmers using “==” and “is”, sometimes even interchangeably. Prima-facie, they look similar and many times you will use any of them. But there is a big difference between them. I remember banging my head to the wall once, just because I had used “is” instead of “==” (thinking that both are same). So, in this post, we will see what is the exact difference between “==” and “is” variable and where and how they should be used?

”==” (Equality)

”==” or equality checks whether two variables have equal value or not?

“is” variable

The “is” variable not only check the values but also the identity of the variable. Hence, it checks whether the variable are identical i.e. they are the same object in the memory and have same object id.

Explaining it using a real-world example

Let us assume that I am at a donut store with my brother and we both are having donuts. I am having a chocolate donut and my brother is having a caramel donut.

'chocolate donut' == 'caramel donut'

# This will result in False

Now assume, that we both are having chocolate donuts, in that case:-

'chocolate donut' == 'chocolate donut'

# This will be true

Since, we are both having a chocolate donut, if we apply the equality check, it will probably leads to True.

However, if we run the “is” check, it should be false. Because I and my brother are having different donuts (though they are from same store and have same ingredients etc.) But if I put some syrup on my donut, it would not change my brother’s donut.

But, if I and my brother would have been sharing the same donut. Then, we might call that donut by different name - ‘my donut’, ‘my brother’s donut’ but that would have been the same object and putting syrup on my donut would have added syrup to my brother’s donut too. So, this is how “is” variable works.

Explaining it using coding example

Let us create two lists with different items and check using the equality variable (“==”)

list_one = [1, 2, 3, 4, 5]
list_two = [1, 2, 5]

if list_one == list_two:
    print(True)

else:
    print(False)

# Output

False

However, if we create two different lists (list_one and list_two) with similar items and check their equality:-

list_one = [1, 2, 3, 4, 5]
list_two = [1, 2, 3, 4, 5]

if list_one == list_two:
    print(True)

else:
    print(False)

# Output

True

However, if we will check these two list with “is” variable, the result will be false:-

list_one = [1, 2, 3, 4, 5]
list_two = [1, 2, 3, 4, 5]

if list_one is list_two:
    print(True)

else:
    print(False)

# Output

False

This is because, though both the lists have exact similar items but they are different lists/objects in the memory. The same can be checked by printing the ids of both the list. You will find out that they are different.

list_one = [1, 2, 3, 4, 5]
list_two = [1, 2, 3, 4, 5]

print(id(list_one))
print(id(list_two))

# Output
4388523912
4388523976

Now, let us create list_one and then assign variable list_two to it.

list_one = [1, 2, 3, 4, 5]
list_two = list_one

if list_one is list_two:
    print(True)

else:
    print(False)

# Output
True

In this case, both the lists are pointing to same memory location.

list_one = [1, 2, 3, 4, 5]
list_two = list_one

print(id(list_one))
print(id(list_two))

print(id(list_one) == id(list_two))

# Output

4455751560
4455751560
True

Now, both the lists point to the same object and since the lists are mutable objects, changing one will change the other two.

list_one = [1, 2, 3, 4, 5]
list_two = list_one

list_one[0] = 6

print(list_one)
print(list_two)

# Output

[6, 2, 3, 4, 5]
[6, 2, 3, 4, 5]

I hope this post explains the difference between “==” and “is” variable in Python. If you have any question, feel free to leave a comment. If you like this post, please share it with your friends.



Related Posts

Nested List to list - Python in just three lines of code
By Udit Vashisht

Nested list to list in Python

Nested list in python can be converted to a flat list in python in just three lines of code. Let us assume that you have a following nested list.

[1, 2, [3, 4, [5, 6]], 7, 8, [9, [10]]]

Now, the above list ...

Read More
Python Logging Module - A Primer
By Udit Vashisht

Logging in python

Logging is the module in the standard library of python. Logging is an important tool for a programmer or developer, as it gives more insights about the code and any errors in it. In this tutorial, we will learn all about the python logging module. ...

Read More
Set Passwords and Secret Keys in Environment Variables (Mac/Linux/Windows) - Python Quicktip
By Udit Vashisht

Hide Passwords and Secret Keys in Environment Variables

If you are into python , there is a fair chance that you would have contributed to open-source or had your code snippets/projects on Github or BitBucket.Some time your code involves some important credentials like passwords or secret keys ...

Read More
Search
Tags
tech tutorials automate python beautifulsoup web scrapping webscrapping bs4 Strip Python3 programming Pythonanywhere free Online Hosting hindi til github today i learned Windows Installations Installation Learn Python in Hindi Python Tutorials Beginners macos installation guide linux SaralGyaan Saral Gyaan json in python JSON to CSV Convert json to csv python in hindi convert json csv in python remove background python mini projects background removal remove.bg tweepy Django Django tutorials Django for beginners Django Free tutorials Proxy Models User Models AbstractUser UserModel convert json to csv python json to csv python Variables Python cheats Quick tips == and is f string in python f-strings pep-498 formatting in python python f string smtplib python send email with attachment python send email automated emails python python send email gmail automated email sending passwords secrets environment variables if name == main Matplotlib tutorial Matplotlib lists pandas Scatter Plot Time Series Data Live plots Matplotlib Subplots Matplotlib Candlesticks plots Tutorial Logging unittest testing python test Object Oriented Programming Python OOP Database Database Migration Python 3.8 Walrus Operator Data Analysis Pandas Dataframe Pandas Series Dataframe index pandas index python pandas tutorial python pandas python pandas dataframe python f-strings padding how to flatten a nested json nested json to csv json to csv python pandas Pandas Tutorial insert rows pandas pandas append list line charts line plots in python Django proxy user model django custom user model django user model matplotlib marker size pytplot legends scatter plot python pandas