The modulo in Python is confusing.
In Python, % operator is calculating the remainder:
%
>>> 9 % 5 4
However:>
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.