7. Functions - Practice Makes Practice


#1


Hello,

My name is Bryan. I recently started learning Python. In the Practice Makes Practice lesson, I figured out the problem, but I don't really understand why the "if number % 3" has to be " == 0." Can someone explain it to me? I have no programming background whatsoever. So, please explain it to me as simple as possible. Thanks.

Bryan


def cube(number):
    return number ** 3
    
def by_three(number):
    if number % 3 == 0:
        return cube(number)
    else:
        return False 

Replace this line with your code.


#2

Hi the modulo % return the remains of a division

9 % 3 remains 0 
10 % 3 remains 1

So if you want to check if a number is divisible by another you will use

if number % 3 == 0:

In this case It check if number is divible by 3


#3

Hello Bryan,
Yes that's because when we use % modulo,it means we are kind of seeing the divisibility bw two integers and when a certain number is divisible by three it will have a remainder equal to 0 hence if you use 9%3,it'll be equal to 0.
so,basically % shows us the remainder.So,if you do 4%3,it must show 1.


#4

As previously explained by @adityawaggy and @wizmarco . The % (modulo) operator checks the divisibility between to number values. If you are asking about the ==0 part of of the statement, the == operator is used to check if one value equals another while = is used for setting the value of a variable based of of another value (whether it'd be an actual value or a variable)


#5

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