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

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?

Related

`is` in python after split gives True with single digit and False for double digits [duplicate]

This question already has answers here:
python is operator behaviour with string [duplicate]
(1 answer)
Python: Why operator "is" and "==" are sometimes interchangeable for strings? [duplicate]
(3 answers)
Closed 2 years ago.
Please do not mark duplicated YET.
I did read this Python is vs ==
I have read around and noticed == vs is that is checks if same memory location and == is used to compare two values but recently I was using is by accident BUT to me it's acting weird and I am don't really understand why though.
I am trying to parse a url path to see if the page number matches for example
a = '9'
b = '/category-1-11-9.html'
c = b.split('.html')[0].split('-')[-1]
print(a is c, ' bool') # this gives True
a = '10'
b = '/category-1-11-10.html'
c = b.split('.html')[0].split('-')[-1]
print(a is c, ' bool') # this gives False
I figured if the number is a single digit, I get True as return and if number is 10 or more I get False
I tried to print both a and c to see their type and value, ended up both typeandvalue` are the same. But why with a single digit it gives True and double digits it gives False?
I am quite confused with this even after reading articles about is
Using python version 3.7.7

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)

double equals vs is in python [duplicate]

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.

Categories