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 :: empty list check in python 
Python :: python remove all occurrence of an items from list 
Python :: python change all values in nested list 
Python :: sort a list python 
Python :: add vertical line in plot python 
Python :: df loc 
Python :: python - login 
Python :: python table code 
Python :: anonymous function python 
Python :: programação funcional python - lambda 
Python :: python escape forward slash 
Python :: are logN and (lognN) same 
Python :: linkedlist python 
Python :: how to loop through an array in python 
Python :: Accessing of Tuples in python 
Python :: django password hashing 
Python :: precision accuracy recall python example 
Python :: python write float with 2 decimals 
Python :: NumPy invert Syntax 
Python :: python get value from list 
Python :: python pandas how to access a column 
Python :: Python NumPy ndarray flat function Syntax 
Python :: python remove  
Python :: Python RegEx Subn – re.subn() Syntax 
Python :: vector data 
Python :: Syntax of Python Frozenset 
Python :: sessions in flask 
Python :: python print text 
Python :: find position of key in dictionary python 
Python :: python list pop equivalent 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =