Python quotient vs remainder

modulopython

The python 2.6 docs state that x % y is defined as the remainder of x / y (http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex). I am not clear on what is really occurring though, as:

for i in range(2, 11):
    print 1.0 % i

prints "1.0" ten times, rather than "0.5, 0.333333, 0.25" etc. as I expected (1/2 = 0.5, etc).

Best Solution

Modulo is performed in the integer context, not fractional (remainders are integers). Therefore:

1 % 1  = 0  (1 times 1 plus 0)
1 % 2  = 1  (2 times 0 plus 1)
1 % 3  = 1  (3 times 0 plus 1)

6 % 3 = 0  (3 times 2 plus 0)
7 % 3 = 1  (3 times 2 plus 1)
8 % 3 = 2  (3 times 2 plus 2)

etc

How do I get the actual remainder of x / y?

By that I presume you mean doing a regular floating point division?

for i in range(2, 11):
    print 1.0 / i