EvilZone
Programming and Scripting => Scripting Languages => : twomakewar October 16, 2014, 09:31:30 AM
-
Hi, i'm new to programming and i was wondering if someone could explain this to me because I'm not understanding what this operand does:
heres the code:
def cube(number):
return number ** 3
def by_three(number):
if number % 3 == 0:
return cube(number)
print "This is True"
else:
return False
print "This is False"
can someone explain this certain piece:
if number % 3 == 0:
- I have taken the liberty of reformatting your post. Please use ['code'] tags next time.
-
if number % 3 == 0
Modular arithmetic. Divide the left by the right and return the remainder. Usually used to "roll over" numbers - to keep the left smaller than the right.
In this particular instance it's being used to check if number is a multiple of three. If the remainder of a division by 3 is zero then number is a multiple of 3.
https://en.wikipedia.org/wiki/Modular_arithmetic
-
If number % 3 == 0:
print("this number is divisible by 3 because there is no remainder")
-
One does not simply... print after you return.
-
but why use this % sign.
-
but why use this % sign.
That is the modulus sign, which tells the interpreter that you want to use modulus.
12 % 4 = 0
12 / 4 = 3
12 % 7 = 5
Do you understand what it does? It gives you the remainder of the devision.
From wiki:
In mathematics, the remainder is the amount "left over" after performing some computation. In arithmetic, the remainder is the integer "left over" after dividing one integer by another to produce an integer quotient (integer division).
-
oh so it divides and returns only the remainder of what you input.
-
If Georgey Has Nine hookers but he can only hook them in pairs, how many hookers will he have left over for himself?
9 % 2 = 1 hookers left for georgey
-
If Georgey Has Nine hookers but he can only hook them in pairs, how many hookers will he have left over for himself?
9 % 2 = 1 hookers left for georgey
I was literally going to post something along these lines, but you beat me to the punch.
-
One does not simply... print after you return.
This made me lol.
But yeah, the function will return before the print happens.