## edXAndrew2012 2 years ago Someone please help me understand how this returns what it returns. The process. I kinda get the first part how variable which is 0 divided by 4 returns 0. but how come it doesnt show 0 for the second if statement and prints 4 first, then 4 then 12 then 16 for variable in range(20): if variable % 4 == 0: print variable if variable % 16 == 0: print ('foo!')

1. celmata

From http://docs.python.org/2/reference/expressions.html : "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 [2]." Now let's examine your programs output: 0 // 0 % 4 == 0 foo! // 0 % 16 == 0 4 // 4 % 4 = 0 8 // 8 % 4 == 0 12 // 12 % 4 == 0 16 // 16 % 4 == 0 foo! // 16 % 16 == 0 Which is indeed correct. The second statement, where variable has a value of 1, does not print any statements because both conditions fail: 1 / 4 == 0.25 1 / 16 == 0.0625 Does this make sense?

2. edXAndrew2012

somewhat...the picture is a little clearer. I read your definition and understand somewhat. Do you think you can make your response more plain (python for dummies). I made the connection with the first 2 lines of output but the rest I still dont.

3. celmata

Have you tried calculating the third iteration of your loop by hand? So, you need to evaluate the statement: (2 % 4) == 0 In certain cases such as this, when you are working with a new operator it can be a good idea to create a test program. In this case you might want to calculate n % 4, given some value of n. This allows you to test your understanding and assumptions.

4. celmata

Something like, for variable in range(20): print str(variable) + ' % 4 = ' + str(variable % 4) print str(variable) + ' % 16 = ' + str(variable % 16) print '\n' for example.

5. edXAndrew2012

Ok after running your example and studying it, I have a better understanding. Thanks for your help.