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.
2.1m questions
2.1m answers
60 comments
57.0k users