Modulo operator


#1

I don’t understand why “== 0” is added/needed after “if number % 3” to complete the instructions of checking whether the number is divisible by three or not. Isn’t “if number % 3” enough?

Where does “== 0” fit in? The instructions don’t explain that and neither does the Hint.


FAQ: Learn Python - Functions - Practice Makes Perfect
#2

9 is divisible by 3. So then you get:

if 9 % 3:

so the remainder is zero. if 0: has no comparison, so python will evaluate 0 it self, which is False. Which is not desired. if 9 % 3 == 0 would be True.


#3

Another question is why do we need “== 0” and get an Invalid syntax error with “= 0” ?

Thank you in advance


#4

Reply to myself from this page :

“”" Notice that the obvious choice for equals, a single equal sign, is not used to check for equality. An annoying second equal sign is required. This is because the single equal sign is already used for assignment in Python, so it is not available for tests.

Warning

It is a common error to use only one equal sign when you mean to test for equality, and not make an assignment! “”"


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.