0
"IS" operator in Python?
Hello, I came across this code while doing Python challenges: a = 257 b = 257 print(a ==b and a is b) The correct answer was True. However, when I tried to check the code in my computer, the answer was False. And when I wrote the same code in an online terminal, the answer was True. I'm really confused! May you please clarfiy the issue! Thank you!
8 Respostas
+ 1
I literally just posted a code about the "is" operator, where you can see that it is a bit confusing.
https://code.sololearn.com/cmVycLQLH0JY/
+ 1
The reason is that "is" compare the objects => the variables refer to different places in memory and they have different id => they are not the same object, but it depends on the size of the numbers.
+ 1
@~ swim ~ Thank you so much. Such helpful answer :)
0
Thank you ! Then the correct answer must be false , unlike what's set in the sololearn quiz, am I right?
0
TheWh¡teCat Thank you! Whats the range of the small numbers?
0
@Loeschzwerg Thank you, I'll check it out :)