[Tutor] Modulus Operator ?
Ken Hammer
kfh777 at earthlink.net
Thu Nov 19 16:11:25 EST 2015
A text for Python 2.7 includes these two paragraphs. The first I understand. The 2nd below mystifies me. How do I use "x % 10" and "x % 100" in the context of the examples I've created following the paragraphs?
"The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another if x % y is zero, then x is divisible by y.
Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly x % 100 yields the last two digits."
I understand these:
x = 49/13
print x
3
y = 49%13
print y
10
z = 0.0 + x + y/13.0
print z
3.76923076923
print 49.0/13
3.76923076923
Thanks, Ken
More information about the Tutor
mailing list