Difference between == and is in python

There are two ways to tell if they are the same in python. == and is. The return values are all Booleans, but what's the difference? That is something I'm curious about.

Conclusion

Simply put, is is comparing both memory locations, while == is simply looking to see if they have the same value. That's all.

For example?

Let's take a look at the code (launch python3 in the terminal)

>>> a = [1, 2, 3]
>>> b = a
>>> b is a 
True
>>> b == a
True
>>> b = a[:]
>>> b is a
False
>>> b == a
True

I will explain. If you assign a list to a and a to b as well, you can see that they are quoting from the same memory location. Therefore, this is True with is. Then use the string operation [:] to duplicate the list. Then, the copy will be completed and it will be different in memory. Therefore, this is False. about it.

When you actually look at the id

>>> id(a)
4364243328
>>> 
>>> id(b)
4364202696

That's right. Since it is False, it is natural that the id is different.

However, there are exceptions like the code below.

>>> a = 256
>>> b = 256
>>> a is b
True
>>> a == b
True
>>>
>>> a = 1000
>>> b = 10**3
>>> a == b
True
>>> a is b
False

What does this mean? Although a and b are assigned numbers separately, the results are different between the top and bottom. Well I will explain. Python does different things for small integers such as 256 to improve performance. It seems that such special integers are stored in the same place with the same id. Then, in 256, it will be the same True regardless of whether you use is or ==, and not for large numbers such as 1000.

It was that. What do you think. If you have any suggestions, please do so.

See you again

Recommended Posts

Difference between == and is in python
About the difference between "==" and "is" in python
Difference between list () and [] in Python
difference between statements (statements) and expressions (expressions) in Python
Difference between @classmethod and @staticmethod in Python
Difference between append and + = in Python list
Difference between nonlocal and global in Python
[python] Difference between variables and self. Variables in class
Difference between Ruby and Python split
Difference between java and python (memo)
Difference between Ruby and Python in terms of variables
Difference between python2 series and python3 series dict.keys ()
Difference between return, return None, and no return description in Python
[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
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
[Python] Difference between class method and static method
[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)
What is the difference between `pip` and` conda`?
What is "functional programming" and "object-oriented" in Python?
The answer of "1/2" is different between python2 and 3
What is the difference between Unix and Linux?
[Introduction to Python] What is the difference between a list and a tuple?
Difference between process and job
Difference between "categorical_crossentropy" and "sparse_categorical_crossentropy"
Find the difference in Python
Difference between regression and classification
[Python] Python and security-① What is Python?
Stack and Queue in Python
Difference between np.array and np.arange
Difference between MicroPython and CPython
Unittest and CI in Python
Identity and equivalence Python is and ==
Difference between ps a and ps -a
Difference between return and print-Python
What is the difference between usleep, nanosleep and clock_nanosleep?
Indent behavior of json.dumps is different between python2 and python3
[python] Calculation of months and years of difference in datetime
Mutual conversion between JSON and YAML / TOML in Python
Compare "relationship between log and infinity" in Gauche (0.9.4) and Python (3.5.1)
Difference in writing method to read external source code between Ruby and Python
What is the difference between a symbolic link and a hard link?
MIDI packages in Python midi and pretty_midi
Difference between SQLAlchemy filter () and filter_by ()
View photos in Python and html
Use fabric as is in python (fabric3)
Sorting algorithm and implementation in Python
Python> Difference between inpbt and print (inpbt) output> [1. 2. 3.] / array ([1., 2., 3.], dtype = float32)