In Learn Python the Hard Way, students encounter modulo (modulus) in exercise 3. Most journalism students find it perplexing. So I thought they might understand it better if they could see it used as a test for prime numbers. See the full code here.
n = raw_input("Enter a number: ") for num in range(2, n):
# below, % is the modulus sign remainder = n % num
# below, % is used with d to represent a variable # called formatters, or format strings: %s %d %r print "%d / %d leaves a remainder of %d" % (n, num, remainder)
if remainder == 0: print "Sorry, %d is not a prime number." % n break
Continuing the PPT series that reviews the lessons learned in Learn Python the Hard Way.
See the first week’s PPT > Exercises 1 - 12
See the second week’s PPT > Exercises 13 - 19
See the third week’s PPT > Exercises 20 - 26
This concludes the four-week series. In the book, there are still more exercises (up to 52).