Open In App

Python Object Comparison : “is” vs “==”

Last Updated : 10 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Both “is” and “==” are used for object comparison in Python. The operator “==” compares values of two objects, while “is” checks if two objects are same (In other words two references to same object).




# Python program to demonstrate working of 
# "=="
  
# Two different objects having same values
x1 = [10, 20, 30]
x2 = [10, 20, 30]
  
# Comparison using "==" operator
if  x1 == x2:
    print("Yes")
else:
    print("No")


Output:

Yes

The “==” operator does not tell us whether x1 and x2 are actually referring to the same object or not. We use “is” for this purpose.




# Python program to demonstrate working of 
# "is"
  
# Two different objects having same values
x1 = [10, 20, 30]
x2 = [10, 20, 30]
  
# We get "No" here
if  x1 is x2:
    print("Yes")
else:
    print("No")
  
# It creates another reference x3 to same list.
x3 = x1
  
# So we get "Yes" here
if  x1 is x3:
    print("Yes")
else:
    print("No")
  
# "==" would also produce yes anyway
if  x1 == x3:
    print("Yes")
else:
    print("No")


Output:

No
Yes
Yes




x1 = [10, 20, 30]
  
# Here a new list x2 is created using x1
x2 = list(x1)
  
# The "==" operator would produce "Yes"
if  x1 == x2:
    print("Yes")
else:
    print("No")
  
# But "is" operator would produce "No"
if  x1 is x2:
    print("Yes")
else:
    print("No")


Output:

Yes
No


Conclusion:

  • “is” returns True if two variables point to the same object.
  • “==” returns True if two variables have same values(or content).



Similar Reads

Python | Data Comparison and Selection in Pandas
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier. The most important thing in Data Analysis is comparing values and selecting data accordingly. The "==" operator works for multiple valu
2 min read
Python | Excel File Comparison
Given Two Excel Files, We want to compare the values of each column row-wise after sorting the values and print the changed column name and row number and values change. Input : Two Excel files Output : Column name : 'location' and Row Number : 0 Column name : 'location' and Row Number : 3 Column name : 'date' and Row Number : 1 Code : Python code
1 min read
Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
Tkinter is a GUI (Graphical User Interface) module which comes along with the Python itself. This module is widely used to create GUI applications. tkinter.ttk is used to create the GUI applications with the effects of modern graphics which cannot be achieved using only tkinter. Checkbutton is used to select multiple options. Checkbuttons can be cr
2 min read
Python | Find Hotel Prices using Hotel price comparison API
Makcorps hotel API is used to get JSON data, to compare Hotel prices, ratings, and reviews from more than 200 websites including; Agoda.com, Hotels.com, Expedia and more. It is organized around GET Requests. One can use this API for free to get information for any hotel or any city regarding prices, ratings, reviews, historical prices and many othe
3 min read
Python | Consecutive String Comparison
Sometimes, while working with data, we can have a problem in which we need to perform comparison between a string and it's next element in a list and return all strings whose next element is similar list. Let's discuss certain ways in which this task can be performed. Method #1 : Using zip() + loop This is one way in which this task can be performe
3 min read
Face Comparison Using Face++ and Python
Prerequisites: Python Programming Language Python is a high-level general-purpose language. It is used for multiple purposes like AI, Web Development, Web Scraping, etc. One such use of Python can be Face Comparison. A module name python-facepp can be used for doing the same. This module is for communicating with Face++ facial recognition service.
3 min read
Comparison of Python with Other Programming Languages
Python is an easily adaptable programming language that offers a lot of features. Its concise syntax and open-source nature promote readability and implementation of programs which makes it the fastest-growing programming language in current times. Python has various other advantages which give it an edge over other popular programming languages su
3 min read
Case-insensitive string comparison in Python
We generally use Python lists to store items. An online shopping application may contain a list of items in it so that the user can search the item from the list of items. For example, Our shopping application has a list of laptops it sells. List contains many brands and one of them is 'Lenovo'. If we want to buy a laptop of Lenovo brand we go to t
4 min read
Python - Similar characters Strings comparison
Given two Strings, separated by delim, check if both contain same characters. Input : test_str1 = 'e!e!k!s!g', test_str2 = 'g!e!e!k!s', delim = '!' Output : True Explanation : Same characters, just diff. positions. Input : test_str1 = 'e!e!k!s', test_str2 = 'g!e!e!k!s', delim = '!' Output : False Explanation : g missing in 1st String. Method #1 : U
6 min read
String Comparison in Python
String comparison is a fundamental operation in any programming language, including Python. It enables us to ascertain strings' relative positions, ordering, and equality. Python has a number of operators and techniques for comparing strings, each with a specific function. We will examine numerous Python string comparison methods in this article an
3 min read
Practice Tags :
three90RightbarBannerImg