Unexpected output with "is" keyword [duplicate] - python

This question already has answers here:
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 4 years ago.
I am unable to figure out the output of the following program
big_num_1 = 1000
big_num_2 = 1000
small_num_1 = 1
small_num_2 = 1
big_num_1 is big_num_2 # False
small_num_1 is small_num_2 # True
What is happening above?
Why is one False and other True.
Source: https://luminousmen.com/post/python-interview-questions-senior

Because is compares the identity of two objects (that is, if they're exactly the same object.) You want to test for equality, and for that you must use the == operator:
big_num_1 == big_num_2
=> True
small_num_1 == small_num_2
=> True
In case you're wondering why this example worked:
small_num_1 is small_num_2
=> True
It's because Python caches small (between -5 and 256) int objects internally, so the objects used in the comparison were taken from the cache and were the same. big_num_1 and big_num_2 are greater than 256, so they're represented by two different objects, and the identity test fails.

Related

Why "`trade".replace("`", "") is "trade" resulting False and "".join(list("`trade")[1:]) is "trade" also False [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 1 year ago.
I wondering why the following tests return False:
Suppose I have 2 simple strings:
str0 = "trade"
str1 = "`trade"
I don't understand why the following tests in python return False:
str1.replace("`", "") is str0
And,
"".join(list(str1)[1:]) is str0 => False
Thanks for your education!
In Python, is compares two objects in memory, == compares their values. Since your two variables are stored at two different places in memory, your comparison using is evaluates to false.

When is it ok to check for literal values with "is" in Python? [duplicate]

This question already has answers here:
Why isn't "is" comparison used in place of "==" for primitive types?
(1 answer)
Boolean identity == True vs is True
(7 answers)
Is there only one True and one False object in Python?
(2 answers)
Closed 2 years ago.
I want to check if a variable is True (not just its truthiness).
This is easily checked like this:
>>> a = 1
>>> isinstance(a, bool) and a
False
>>> a = True
>>> isinstance(a, bool) and a
True
Of course, that is totally fine. However, the same behaviour can be seen in the following code:
>>> a = 1
>>> a is True
False
>>> a = True
>>> a is True
True
In my opinion, the second snippet is a bit more readable (if you understand how is works). With Python 3.8, I get a SyntaxWarning when executing the second snippet. Would the second example work with all Python implementations? Is there a guarantee that bool objects of the same value are identical in this way? I believe I have read somewhere that this is not guaranteed for int objects. Is it ever ok to check literal values with is like this?

Magic Number in Python [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
Closed 4 years ago.
In the following code :
magicnumber = 256
for n in range (500):
if n is magicnumber:
print ("the magic number is " , n)
break
else:
print(n)
The loop breaks at 256, but if you set magicnumber to 257, it doesn't. Why ?
Because you are checking for identity with is (instead of checking for equality with ==).
As an implementation detail, Python keeps an array of integer objects for all integers
between -5 and 256, when you create an int in that range you actually just get
back a reference to the existing object.
So integers above 256 will still be equal, but not identical (unless they refer to the same object, you can compare that with id()).
Source: https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong

Different behaviors of 'is' operator when comparing variables with same int values [duplicate]

This question already has answers here:
Is there a difference between "==" and "is"?
(13 answers)
"is" operator behaves unexpectedly with integers
(11 answers)
Closed 4 years ago.
I'm playing with the 'is' operator in the interactive shell when I encountered an odd behavior with the below code:
It goes as expected at first:
>>> x = 11
>>> y = 11
>>> x is y
True
But when I tried this one:
>>> x = 987456
>>> y = 987456
>>> x is y
False
After further tries using id() function, I noticed that integers >256 points on the same object while others are not. I also noticed that this behavior only occurs in the python interactive shell. What's with this behavior?
is checks for memory address. Immutable objects that are wrappers around C type tends to have same memory address, whereas others don't. The difference here is the bytes required to store the integers.

Python is vs == [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
String comparison in Python: is vs. ==
When is the == operator not equivalent to the is operator? (Python)
I'm pretty new to Python still. I heard someone say use is, not == because "this isn't C". But I had some code x is 5 and it was not working as expected.
So, following proper Python/PEP style, when is the time to use is and when is the time to use == ?
You should use == to compare two values. You should use is to see if two names are bound to the same object.
You should almost never use x is 5 because depending on the implementation small integers might be interned. This can lead to surprising results:
>>> x = 256
>>> x is 256
True
>>> x = 257
>>> x is 257
False
The two operators have different meaning.
is tests object identity. Do the two operands refer to the same object?
== tests equality of value. Do the two operands have the same value?
When it comes to comparing x and 5 you invariably are interested in the value rather than the object holding the value.

Categories

Resources