What are you comparing with Python is and ==?

Well, I think that the fact that they are not the same is fairly pervasive, but Then __ I would like to explain what the hell are you comparing __.

It seems that there are always mistakes in the article, so I'd be happy if you could point it out (I'd be happy if there was feedback)

Difference between == and is

I will explain it briefly (if you understand it, you can skip it) Look at Example 1 first

Example 1


a = "Rabbit house"
b = "Rabbit house" #Same string as a
c = "Kantoan" #Character string different from a

#a and c==Compare with
print(a == c) # False
#Compare a and c with is
print(a is c) # False

#a and b==Compare with
print(a == b) # True
#Compare a and b with is
print(a is b) # False

Rabbit House and Kantoan are naturally different, so ʻa == c and ʻa is c are both False.

On the other hand, ʻaandblook the same at first glance ... True when ʻa == b False when ʻa is b`

The result is different

What do you mean

Well the main subject

I will explain based on Example 2.

Example 2


a = "Rabbit house"
b = "Rabbit house" #Same string as a

# ==Compare with
print(a == b) # True
#Compare with is
print(a is b) # False

print(id(a)) #object id of a
print(id(b)) #object id of b(Returns a value that is not the same as a)

In Python, all data is an object. ʻId () `is a function that returns the id of the argument object (hereinafter referred to as the object id).

== returned True because ʻa and b were __the same string __. However, ʻa is b returned False, even though it was the same string. This is because ʻis` is a __ operator that compares __ object ids.

Checks if == is __equivalent __. On the other hand, ʻis` is checking if it is __ident __.

The difference is here

Tips (may be useful to remember)

--The object id returned by the id () function becomes the address of the memory in which the object resides when multiplied by hex (). --There are exception cases (see below)

exception

It is enough to understand the above contents, but there are exception cases, so I will introduce it for the time being.

Example 3


a = 100
b = 100

print(a == b) #Is it equivalent
print(a is b) #Is it the same

Will it be True or False?

Those who understand so far are probably You would expect ʻa == b to return True and ʻa is b to return False.

But __ both are True ... __

What? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ??

*** These comparison operators should be generalized, but there are exceptions and they are crazy. *** ***

I would think. I thought so at first (´ ・ ω ・ `)

However, there is a good reason for this. From Plain Integer Objects — Python 2.7.13 documentation --PyObject * PyInt_FromLong

The current implementation keeps an array of integer objects for all integers between -5 and 256

To explain in Japanese

The current implementation of Python holds an array of integer objects from -5 to 256

There is. So Python always keeps arrays from -5 to 256 in memory.

Verification

a = b = range(-6, 258) #-Generate an array from 6 to 257

for x,y zip(a, b):
    print(x, x is y) #Compare if two equivalent values are the same
result
-6 False
-5 True
-4 True
.
.
(Omission)
.
.
255 True
256 True
257 False

From -5 to 256, you returned True because the object ids are the same. On the other hand, it returns False for -6 and 257 (first and last values) that are out of the range of values held by memory.

It's confusing, but you should remember this too.

Other The is comparison between Trues and between Falses always returns True. This is because there is always only one True and False in memory.

There is another exception as far as I know, but I will omit it here.

If you have any suggestions, please leave them in the comments section!

Recommended Posts

What are you comparing with Python is and ==?
What are you using when testing with Python?
[Python] Python and security-① What is Python?
[Python] What is a with statement?
[Python] What are @classmethods and decorators?
Python | What you can do with Python
What are you comparing with Python is and ==?
Checking if you are Sudoku
What is python
What is Python
[Python] What is pandas Series and DataFrame?
What is "functional programming" and "object-oriented" in Python?
[Python] What is Pipeline ...
[Python] What is virtualenv
What are python tuples and * args after all?
What is pip and how do you use it?
What is God? Make a simple chatbot with python
What you can and cannot do with Tensorflow 2.x
Encryption and decryption with Python
Python and hardware-Using RS232C with Python-
[Python] * args ** What is kwrgs?
python with pyenv and venv
Identity and equivalence Python is and ==
What is a python map?
Works with Python and R
Python Basic Course (1 What is Python)
What to do if you run python in IntelliJ and end with an error
What you can do with the Python standard library statistics
[Python] What do you do with visualization of 4 or more variables?
[Python] What is a tuple? Explains how to use without tuples and how to use it with examples.
Python a + = b and a = a + b are different
Shining life with Python and OpenCV
[Python] What is a zip function?
Robot running with Arduino and python
Neural network with OpenCV 3 and Python 3
AM modulation and demodulation with python
Difference between == and is in python
[Python] font family and font with matplotlib
Scraping with Node, Ruby and Python
What are "sudo ln -s" and "ln -s"?
Nice to meet you with python
Scraping with Python, Selenium and Chromedriver
Scraping with Python and Beautiful Soup
[Python] What is @? (About the decorator)
[python] What is the sorted key?
(Beginner) What are cores and threads?
JSON encoding and decoding with python
Python for statement ~ What is iterable ~
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
What to do with PYTHON release?
Reading and writing NetCDF with Python
I played with PyQt5 and Python3
Are you having trouble with "pipenv.exceptions.ResolutionFailure"?
What is the python underscore (_) for?
Reading and writing CSV with Python
Python> What is an extended slice?
Multiple integrals with Python and Sympy
Until you run python with apache
Coexistence of Python2 and 3 with CircleCI (1.0)
Easy modeling with Blender and Python
Sugoroku game and addition game with python