The Wayback Machine - https://web.archive.org/web/20240722204606/https://www.geeksforgeeks.org/python-object-comparison-is-vs/
Open In App

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

Last Updated : 10 Sep, 2018
Comments
Improve
Suggest changes
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 | 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
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
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
Chaining comparison operators in Python
Checking more than two conditions is very common in Programming Languages. Let's say we want to check the below condition: a < b < c The most common syntax to do it is as follows: if a < b and b < c : {...} In Python, there is a better way to write this using the Comparison operator Chaining. The chaining of operators can be written as
5 min read
Comparison Operators in Python
The Python operators can be used with various data types, including numbers, strings, booleans, and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters (lexicographic order).Be cautious when compa
5 min read
Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
Prerequisite: Object-Oriented Programming in Python | Set 1 (Class, Object and Members) Data hiding In Python, we use double underscore (Or __) before the attributes name and those attributes will not be directly visible outside. Python Code class MyClass: # Hidden member of MyClass __hiddenVariable = 0 # A member method that changes # __hiddenVari
3 min read
Comparison of Java with other programming languages
Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet, Java is used in ev
4 min read
Python2 vs Python3 | Syntax and performance Comparison
Python 2.x has been the most popular version for over a decade and a half. But now more and more people are switching to Python 3.x. Python3 is a lot better than Python2 and comes with many additional features. Also, Python 2.x is becoming obsolete this year. So, it is now recommended to start using Python 3.x from now-onwards. Still in dilemma? Ev
4 min read
When to Use Django? Comparison with other Development Stacks
Prerequisite - Django Introduction and Installation When to Use Django and Why? After getting to know the basics of Python, one should know when to use Django and why? Django is a high-level python based web framework which allows you to quickly create web applications without all of the installation or dependency problems that you normally will fi
4 min read
Practice Tags :
three90RightbarBannerImg