numerical-computing-is-fun icon indicating copy to clipboard operation
numerical-computing-is-fun copied to clipboard

Use == instead of 'is' for comparing number

Open progval opened this issue 8 years ago • 2 comments

Hi,

In this notebook: https://github.com/mikkokotila/jupyter4kids/blob/master/notebooks/numerical-computing-is-fun-2.ipynb you use the 'is' keyword to compare numbers.

This should be avoided, because Python's semantic does not guarantee that '1 is 1'. In fact, in the CPython implementation, this is only True for integers lower or equal to 256:

>>> a = 257
>>> b = 257
>>> a is b
False
>>> a = 256
>>> b = 256
>>> a is b
True

progval avatar Oct 07 '17 12:10 progval

Very interesting, thank you very much. I will make the change in the current notebooks, and keep this in mind in the coming ones. Thanks a lot! :)

mikkokotila avatar Nov 12 '17 06:11 mikkokotila

I realised 'is' was still in place. So I checked above issue, having no problem with higher numbers, unless I assigned numbers using a = 257 and b =257, which the notebook doesn't do (maybe it did before).

Adding this comment for benefit of others... Anyone interested in the reasons can find why on stackoverflow.

https://stackoverflow.com/questions/306313/is-operator-behaves-unexpectedly-with-integers

@mikkokotila thanks for the book, going to use it in a math lesson tomorrow

Doc-Scott avatar Jul 08 '18 14:07 Doc-Scott