Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

is vs == python

The Equality operator (==) compares the values of both the operands 
and checks for value equality. 
Whereas the ‘is’ operator checks whether both the operands refer to the 
same object or not (present in the same memory location).
Comment

Python == vs is

In general avoid using is , as it has unpredictable behaviour
# hence avoid using is but below is general theory on is

== is for value equality. Use it when you would like to know if two objects have the same value.
is is for reference equality. Use it when you would like to know if two references refer to the same object.
In general, when you are comparing something to a simple type, you are usually checking for value equality,
so you should use ==. For example, the intention of your example is probably to check 
whether x has a value equal to 2 (==), not whether x is literally referring to the same object as 2.

Something else to note: because of the way the CPython reference implementation works, 
you'll get unexpected and inconsistent results if you mistakenly use is to compare for reference equality on integers:

>>> a = 500
>>> b = 500
>>> a == b
True
>>> a is b
False
That's pretty much what we expected: a and b have the same value, but are distinct entities. But what about this?

>>> c = 200
>>> d = 200
>>> c == d
True
>>> c is d
True
This is inconsistent with the earlier result. What's going on here
it turns out the reference implementation of Python caches 
integer objects in the range -5..256 as singleton instances for performance reasons. Here's an example demonstrating this:

>>> for i in range(250, 260):
a=i
print( "%i: %s" % (i, a is int(str(i))) )
... 
250: True
251: True
252: True
253: True
254: True
255: True
256: True
257: False
258: False
259: False
This is another obvious reason not to use is: the behavior is left up to implementations when you're erroneously using it for value equality.
Comment

PREVIOUS NEXT
Code Example
Python :: load python file in jupyter notebook 
Python :: mapping with geopandas 
Python :: which function to use in random module for a list in python 
Python :: python *x 
Python :: rename files with spaces in a folder python 
Python :: Selecting subset of columns with pandas 
Python :: distance matrix gogle map python 
Python :: how to change values of dictionary in python 
Python :: matplotlib larger chart 
Python :: how to remove some characters from a string in python 
Python :: numpy transpose 
Python :: Filter Pandas rows by specific string elements 
Python :: calculate perimeter of rectangle in a class in python 
Python :: Rectangle with python 
Python :: python: convert variable as character 
Python :: python if elif else 
Python :: python regex search a words among list 
Python :: if string in lost py 
Python :: torch tensor to pandas dataframe 
Python :: dicttoxml python? 
Python :: python get nested dictionary keys 
Python :: defaultdict initialize keys 
Python :: pandas line plot dictionary 
Python :: df sort by column names 
Python :: pandas df to list of dictionaries 
Python :: python undefined 
Python :: numpy multiply element wise 
Python :: pandas apply 
Python :: arrayfield in django 
Python :: matplotlib legend get handles 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =