The modulo in Python is confusing.
In Python, % operator is calculating the remainder:
%
>>> 9 % 5 4
However:
>>> -9 % 5 1
Why is the result 1? and not -4?
1
-4
Because in python, the sign matches the denominator.
>>> 9 % -5 -1 >>> -9 % 5 1
For an explanation of why it was implemented this way, read the blog post by Guido.
1.4m articles
1.4m replys
5 comments
57.0k users