eka-foundation / numerical-computing-is-fun

Learning numerical computing with notebooks for all ages.
764 stars 115 forks source link

Use == instead of 'is' for comparing number #4

Open progval opened 6 years ago

progval commented 6 years ago

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
mikkokotila commented 6 years ago

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! :)

Doc-Scott commented 6 years ago

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