# Is % modulo different than / division?

In the solution, how does:

if number % 3 == 0:

Ensure that the number is divisible by 3, and not just ensure that the number is 3? Surely if I call:

by_three(9)

The function should return False, because 9 / 3 = 3, not 0. Yet when I tried this i got 729 as an output.

There’s clearly something I’m missing surrounding the == 0 part of the code but I’m not sure what?

9 Likes

this condition uses the modulo operator (`%`), which gives the remainder, which is different from division operator (`/`)

16 Likes

Of course! I’m a tired idiot, thanks!

9 Likes

we can also use:
number / 3 == 3
right?

How could we figure this out? we first need to determine the objective, so numbers divisible by 3 (3, 6, 9 and so forth) should result in True, the rest in false. So now we could put your code to the test with a simple loop:

``````for i in range(15):
print(i, i / 3 == 3)
``````

and see if the output is what we expected. Does it match your/our expectations/prognoses? If so, good. If not, why not?

4 Likes

Thank You …finally understood modullo is used fro getting remainder …I mean seriously the othr sites have explained it in such a bizzare way .

Hi. I did

``````def cube(number):

return number * number * number

def by_three (number):

if cube(number) % 3 == 0:

return cube(number)

else:

return False
``````

And codeacademy accepts it. However, if i do print cube(2) , i will get 8 instead of False. Shouldn’t it be False since 8 is not divisible by 3 ?

no, `cube` function always a number.

if you did `by_three(2)` then you would get False. Seems you called the wrong function

1 Like

Right… I just realised that. Thank you !

Hi guys, why are we using % in this particular exercise when it looks for remainders? Why is this more beneficial than a / operator? Secondly, why do we use ‘if number % 3 == 0:’? Thirdly, when do we use if and else?

because the modulo operator (`%`) gives you the remainder, while the division operator (`/`) simply give us the result of the division

because this condition is only true for numbers divisible by 3 (when the remainder is zero)

probably because we want to have different behavior for numbers divisible and not divisible by three