Modulo operator | SoloLearn: Learn to code for FREE!


Modulo operator

I typed, >> print(-28 % 10) Output:- 2 >> print(28 % -10) Output:- -2 >> print(-28 % -10) Output:- -8 Anybody please explain how this is working?? How we get -8 here

3/11/2020 7:26:05 AM

Mohit Chindalia

1 Answer

New Answer


"The % (modulo) operator yields the remainder from the division of the first argument by the second. The numeric arguments are first converted to a common type. A zero right argument raises the ZeroDivisionError exception. The arguments may be floating point numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.) The modulo operator always yields a result with the same sign as its second operand (or zero); the absolute value of the result is strictly smaller than the absolute value of the second operand."