Python "is" v/s "=" weird behavior [duplicate] - python

This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 9 years ago.
In Python, x is y returns true if the two sides evaluate to the same object; that is, it checks pointer equality. Now I tried something like this:
>>>a = 2
>>>a is 2
True
However..
>>>a = 9203409249024
>>>a is 9203409249024
False
Why this difference in behavior?

Python (and other languages) cache small numbers in memory, so that multiple variables that have the value (for example) 2 will point to the same location in memory (and, hence, is returns True).

Related

== 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.

Basic OR difference [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 5 years ago.
I was doing some tests, when I tried:
len(pin) == (4 or 6)
Half of the tests failed.
However with :
(len(pin) == 4 or len(pin) == 6)
All the tests were passed.
I am unable to understand the difference that is between these two.
pin is usually a number like 1234 or 12345.
That is because according to precedence rules, the right hand side expression is evaluated first in your first condition i.e.
len(pin) == (4 or 6)
Here, first (4 or 6) is evaluated and returns 4 (or true in some languages). Now, only those cases return true where length actually is 4.
Your second condition works as expected, because it compares the length to 4 and 6 both separately and then applies an or on both the boolean values.

If "a is not b" how to know what is the difference? [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 6 years ago.
Two items may be unequal in many ways. Can python tell what is the reason?
For example: 5 is not 6, int(5) is not float(5), "5" is not "5 ", ...
Edit: I did not ask what kinds of equality test there are, but why and how those are not equal. I think my question is not duplicate.
There are several checks you can do:
# Both variables point to the same object (same memory space)
a is b
# Both variables evaluates to the same value
a == b
# Both variables are of same type
type(a) == type(b)
is checks if the two items are the exact same object. This check identity
== checks if the two objects are equal values
You use is not None to make sure that the object the "real" none and not just false-y.

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)

Why is is-operator not functioning like ==? [duplicate]

This question already has answers here:
Why does comparing strings using either '==' or 'is' sometimes produce a different result?
(15 answers)
Closed 9 years ago.
If I understand correctly, the is operator can take the place of ==.
Why when I write
if inpty == "exit":
return
does the function exit, but when I write
if inpty is "exit":
return
the function does not?
inpty is the value of the input.
is compares identity, whereas == compares equality.
In other words, a is b is the same as id(a) == id(b).
because in this case, the is operator is testing identity, not the value.

Categories