Modulo in Python: Difference between revisions
(3 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
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 <ref>https://docs.python.org/2/reference/expressions.html</ref> | 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 <ref>https://docs.python.org/2/reference/expressions.html</ref> | ||
We most often use modulo to test if a number is odd, even, or something like that. | |||
==Example of modulo in Python== | ==Example of modulo in Python== | ||
Line 9: | Line 11: | ||
<syntaxhighlight lang="python" > | <syntaxhighlight lang="python" > | ||
# | # this code sample will help us understand how to use modulo in Python | ||
# our first function asks "is this number divisble by 2?". If it isn't, it must be odd. | |||
def is_it_odd(number): | |||
if number % 2 == 0: | |||
print("is even") | |||
else: | |||
print("is odd") | |||
return | |||
is_it_odd(1) | |||
is_it_odd(2) | |||
is_it_odd(3) | |||
is_it_odd(4) | |||
</syntaxhighlight> | |||
Another example: | |||
<syntaxhighlight lang="python" > | |||
# this code sample will help us understand how to use modulo in Python | |||
# Another example might be to see if a number is divisible by 25 | |||
def is_it_divisible_by_25(number): | |||
if number % 25 == 0: | |||
print("is divisble by 25") | |||
else: | |||
print("is not divisble by 25") | |||
return | |||
is_it_divisible_by_25(125) | |||
is_it_divisible_by_25(275) | |||
is_it_divisible_by_25(300) | |||
is_it_divisible_by_25(431) | |||
is_it_divisible_by_25(25) | |||
is_it_divisible_by_25(305) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 13:46, 21 March 2016
Introduction[edit]
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]
We most often use modulo to test if a number is odd, even, or something like that.
Example of modulo in Python[edit]
# this code sample will help us understand how to use modulo in Python
# our first function asks "is this number divisble by 2?". If it isn't, it must be odd.
def is_it_odd(number):
if number % 2 == 0:
print("is even")
else:
print("is odd")
return
is_it_odd(1)
is_it_odd(2)
is_it_odd(3)
is_it_odd(4)
Another example:
# this code sample will help us understand how to use modulo in Python
# Another example might be to see if a number is divisible by 25
def is_it_divisible_by_25(number):
if number % 25 == 0:
print("is divisble by 25")
else:
print("is not divisble by 25")
return
is_it_divisible_by_25(125)
is_it_divisible_by_25(275)
is_it_divisible_by_25(300)
is_it_divisible_by_25(431)
is_it_divisible_by_25(25)
is_it_divisible_by_25(305)
Other ways to understand this[edit]
Click here for a video - this is a basic example