Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
progval opened this issue Oct 7, 2017 · 2 comments
Open

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

progval opened this issue Oct 7, 2017 · 2 comments

Comments

@progval
Copy link

progval commented Oct 7, 2017

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
Copy link
Collaborator

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants