I have tried the same code in python, once ran as a .py
file and once typed in IDLE, but it gives different output for the same code:
a = 3.4
b = 3.4
a is b
I have attached a screenshot taken while trying by both methods:
I have tried the same code in python, once ran as a .py
file and once typed in IDLE, but it gives different output for the same code:
a = 3.4
b = 3.4
a is b
I have attached a screenshot taken while trying by both methods:
The reason for why your left window returns false and the right true is because of each command you type is a block, as stated in the manual:
A Python program is constructed from code blocks. A block is a piece of Python program text that is executed as a unit. The following are blocks: a module, a function body, and a class definition. Each command typed interactively is a block.
Thus when you are using your console, each individual command you type is considered a block. Each block has constants which are reused. In your case 3.4 is a constant. But when you are typing the second command, it is considered a new block so it won't find a constant which it can reuse. In the second case of using a .py file the constant is saved and reused, because a file is seen as a single code block.
A way for you to check this is to declare both variables on the same line like this:
>>> a = 3.4; b = 3.4;
>>> print(a is b)
This will output True, because you declare both variables in the same command, thus block.
If you however are trying to just compare two variables you should use the ==
. Keep in mind that you are doing floating point comparison, check out this stackoverflow on how to best do that: floating point comparison in Python