About the difference between "==" and "is" in python

Introduction

There are "==" and "is" as a method to compare objects, but I didn't know how to use them properly, so I summarized the differences.

About "=="

Compare whether object 1 and object 2 are equivalent. Equivalence means "does it have the same value?"

test01.py


object1=100
object2=100

if object1 == object2 :
    print("Equivalent")
else :
    print("Not equivalent")

#Output: Equivalent

In test01.py, object1 and object2 have the same value (int type 100), so the if statement is evaluated as True.

test02.py


object1=100    #int type 100
object2="100"  #str type 100

if object1 == object2 :
    print("Equivalent")
else :
    print("Not equivalent")

#Output: not equivalent

Since test02.py compares "int type 100" and "str type 100", the if statement is evaluated as False.

About "is"

"Is" checks "whether the compared objects are the same object".

test3.py


object1=[1,2,3]
object2=[1,2,3]

print(object1 is object2)
print(object1 is object1)

#output
#False
#True

When an object is created, an id number (unique number) is assigned to the object. "Is" compares this id to see if they are the same object. The id number can be found with the id method.

test04.py


object1=[1,2,3]
object2=[1,2,3]

print(id(object1))    #id()Look up the id number of that object with
print(id(object2))

print(object1 is object2)
print(object1 is object1)

#output
#4488767496    (id number of object1)
#4488768392    (id number of object2)
#False
#True

Summary

"==" Checks if the compared objects are equivalent. "Is" checks if the ids of the compared objects are equivalent.

Recommended Posts

About the difference between "==" and "is" in python
Difference between == and is in python
Difference between list () and [] in Python
difference between statements (statements) and expressions (expressions) in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
What is the difference between `pip` and` conda`?
[python] Difference between variables and self. Variables in class
About the difference between PostgreSQL su and sudo
What is the difference between Unix and Linux?
[Introduction to Python] What is the difference between a list and a tuple?
[Python] Sweet Is it sweet? About suites and expressions in the official documentation
Difference between Ruby and Python in terms of variables
What is the difference between usleep, nanosleep and clock_nanosleep?
Difference between return, return None, and no return description in Python
Difference between Ruby and Python split
Difference between java and python (memo)
[Python] What is @? (About the decorator)
Difference between python2 series and python3 series dict.keys ()
[Python] Difference between function and method
Python --Difference between exec and eval
[Python] Difference between randrange () and randint ()
[Python] Difference between sorted and sorted (Colaboratory)
Python module num2words Difference in behavior between English and Russian
List concatenation method in python, difference between list.extend () and β€œ+” operator
Get the current date and time in Python, considering the time difference
[Python] Explain the difference between strftime and strptime in the datetime module with an example
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Difference between PHP and Python finally and exit
What is "mahjong" in the Python library? ??
[Python] Difference between class method and static method
About the relationship between Git and GitHub
[Python Iroha] Difference between List and Tuple
[python] Difference between rand and randn output
Differences in multithreading between Python and Jython
How to use is and == in Python
Difference in how to write if statement between ruby ​​and python
Transcendental simple and clear! !! Difference between single quotes and double quotes in Python
File open function in Python3 (difference between open and codecs.open and speed comparison)
Summary of the differences between PHP and Python
[Xonsh] The Python shell is sharp and god
How to get the date and time difference in seconds with python
I tried to find out the difference between A + = B and A = A + B in Python, so make a note
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
[Python] Comprehension notation. Write a for statement simply. (A collection is the difference between an iterator and an iterator)
About __all__ in python
About the * (asterisk) argument of python (and itertools.starmap)
Difference in writing method to read external source code between Ruby and Python
Consideration of the difference between ROC curve and PR curve
Check if the string is a number in python
The rough difference between Unicode and UTF-8 (and their friends)
The simplest Python memo in Japan (classes and objects)
Investigate the relationship between TensorFlow and Keras in transition
Receive the form in Python and do various things
Can BERT tell the difference between "candy (candy)" and "candy (rain)"?
Carefully understand the exponential distribution and draw in Python
Indent behavior of json.dumps is different between python2 and python3
Plot and understand the multivariate normal distribution in Python