Open study

is now brainly

With Brainly you can:

  • Get homework help from millions of students and moderators
  • Learn how to solve problems with step-by-step explanations
  • Share your knowledge and earn points by helping other students
  • Learn anywhere, anytime with the Brainly app!

A community for students.

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!')

Computer Science
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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.

Join Brainly to access

this expert answer

SIGN UP FOR FREE
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?
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.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

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.
Ok after running your example and studying it, I have a better understanding. Thanks for your help.

Not the answer you are looking for?

Search for more explanations.

Ask your own question