I am exploring python is vs ==, when I was exploring it, I find out if I write following;
>>> a = 10.24
>>> b = 10.24
in a python shell and on typing >> a is b, it gives me output as false. But when I write the following code in a python editor and run it I get true.
a = 10.24
b = 10.24
print(a is b)
Can anyone explain why I am getting two different results of the same variables and expression?
You should not rely on is for comparison of values when you want to test equality.
The is
keyword compares id's of the variables, and checks if they are the same object. This will only work for the range of integers [-5,256] in Python, as these are singletons (these values are cached and referred to, instead of having a value stored in memory). See What's with the integer cache maintained by the interpreter? This is not the same as checking if they are the same value.
As for why it behaves differently in a REPL environment versus a passed script, see Different behavior in python script and python idle?. The jist of it is that a passed script parses the entire file first, while a REPL environment like ipython or an IDLE shell reads lines one at a time. a=10.24
and b=10.24
are executed in different contexts, so the shell doesn't know that they should be the same value.