double equals vs is in python [duplicate] - python

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 10 years ago.
I run the following in the Python interpreter:
>>> foo = 10
>>> dir(foo) == dir(10)
True
>>> dir(foo) is dir(10)
False
>>>
Why is this?

is checks that 2 arguments refer to the same object, == checks that 2 arguments have the same value. dir() returns a list which contains the same data for both foo and 10, but the actual list instances for the 2 things are different.

Related

'is' operation in terminal and in Pycharm [duplicate]

This question already has answers here:
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
String comparison in Python: is vs. == [duplicate]
(4 answers)
Closed 3 years ago.
I am aware of the difference of 'is' operator with strings. However, when I type the following example in Pycharm I get always True.
a = 'a monkey'
b = 'a monkey'
print(a is b)
I get:
True
Where in terminal:
>>> a = 'a monkey'
>>> b = 'a monkey'
>>> a is b
False
Why is this happening?
Edit: Why Pycharm returns always True?

== and is not returning the same even though comparing an int [duplicate]

This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Is there a difference between "==" and "is"?
(13 answers)
Closed 3 years ago.
I'm using the pow function and found out I had a bug in my code because == and is were not having the same behavior.
Here goes an example: pow(3, 47159012670, 47159012671) == 1 returns True but pow(3, 47159012670, 47159012671) is 1 returns False.
I'd like to know what is it that I'm not getting.
The difference between is and == is:
a1 is a2 # return True
only when they share the same location or id in memory, that means when id(a1) and id(a2) are the same. I hope this will help more.

New string after concatenation refers to an existing object [duplicate]

This question already has answers here:
Why variables holding same value has the common ID in Python? [duplicate]
(1 answer)
About the changing id of an immutable string
(5 answers)
Closed 5 years ago.
Here's my snippet of code
>>> a = "some_string"
140420665652016
>>> id(a)
>>> id("some" + "_" + "string")
140420665652016
Notice that both the ids are same. But this does not happen with integers (which are also immutable like strings).
>>> a = 999
>>> id(a)
140420666022800
>>> id(999)
140420666021200
>>> id(998 + 1)
140420666023504
I'm not able to find a reason of why it's happening only with strings.

Why does Python create different objects for the same integer in the console? [duplicate]

This question already has answers here:
About the changing id of an immutable string
(5 answers)
The `is` operator behaves unexpectedly with non-cached integers
(2 answers)
Closed 6 years ago.
a = 1234
b = 1234
print(id(a), id(b))
In the above script both IDs are the same. a and b refer to the same object. When running the commands one by one in a Python console the IDs are different:
>>> a = 1234
>>> b = 1234
>>> print(id(a), id(b))
2445680 6579168
but:
>>> print(id(1234), id(1234))
6579472 6579472
Why is 1234 a different object in successive commands, but not in one command or in a script?
I tested this on CPython 3.5.2.

Why don't these statements both return 'true'? [duplicate]

This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 7 years ago.
So I came across some strange behaviour in python :
>>> 2+1 is 3
True
>>> 2000+1 is 2001
False
It doesn't use the correct logic when large integers are used, why is this?
is will return True if two variables point to the same object. So that there id.
In [21]: id(3)
Out[21]: 15538056
In [22]: id(2+1)
Out[22]: 15538056
In [23]: id(2001), id(2000+1)
Out[23]: (52399576, 54526360)

Categories