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!')
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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 ." 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?
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.
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.