"IS" operator in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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!

31st Jul 2019, 3:04 PM
N. A.
N. A. - avatar
7 Answers
+ 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/
31st Jul 2019, 5:26 PM
Loeschzwerg
+ 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.
31st Jul 2019, 3:10 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
N. A. but for small numbers Python uses the same object instance so that's why the answer is true.
31st Jul 2019, 3:29 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
@~ swim ~ Thank you so much. Such helpful answer :)
1st Aug 2019, 1:31 PM
N. A.
N. A. - avatar
0
Thank you ! Then the correct answer must be false , unlike what's set in the sololearn quiz, am I right?
31st Jul 2019, 3:13 PM
N. A.
N. A. - avatar
0
TheWh¡teCat Thank you! Whats the range of the small numbers?
31st Jul 2019, 3:33 PM
N. A.
N. A. - avatar
0
@Loeschzwerg Thank you, I'll check it out :)
1st Aug 2019, 1:32 PM
N. A.
N. A. - avatar