-1

I understand that the Modulo function returns the remainder of a division problem. Ex: 16 % 5 = 3 with a remainder of 1. So 1 would be returned.

>>> 1 % 3      Three goes into 1 zero times remainder 1
1
>>> 2 % 3      Three goes into 2 zero times remainder 2
2
>>> 0 % 3      What happens here?  3 goes into zero, zero times remainder 3 

if we follow the logic of the previous two illustrations, that is not what was returned, zero was. Why?

>>> 0 % 3 
0
AChampion
  • 29,683
  • 4
  • 59
  • 75
Jeff Schneider
  • 11
  • 1
  • 1
  • 3

2 Answers2

1

The Python % operator is defined so that x % y == x - (x // y) * y, where x // y = ⌊x / y⌋. For positive integers, this corresponds to the usual notion of the “remainder” of a division. So, for any y ≠ 0,

0 % y
= 0 - ⌊0 / y⌋ * y      by definition of %
= 0 - ⌊0⌋ * y          because 0 divided by anything is 0
= 0 - 0 * y            because 0 is an integer, so floor leaves it unchanged
= 0 - 0                because 0 times anything is 0
= 0
dan04
  • 87,747
  • 23
  • 163
  • 198
0

Look at it again:

1 % 3 is 0 remainder 1 =>  1 = 3*0 + 1  
2 % 3 is 0 remainder 2 =>  2 = 3*0 + 2
0 % 3 is 0 remainder 0 [not 3] because 0 = 3*0 + 0

why are you taking what remains after the division in the first two cases but not the last?