EvilZone

Programming and Scripting => Scripting Languages => : twomakewar October 16, 2014, 09:31:30 AM

: Python help
: 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.
: Re: Python help
: Fur October 16, 2014, 09:49:59 AM
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
: Re: Python help
: Stackprotector October 16, 2014, 10:23:14 AM
If number % 3 == 0:
    print("this number is divisible by 3 because there is no remainder")
: Re: Python help
: Kulverstukas October 16, 2014, 12:25:25 PM
One does not simply... print after you return.
: Re: Python help
: twomakewar October 16, 2014, 07:51:58 PM
but why use this % sign.
: Re: Python help
: Phage October 16, 2014, 09:26:40 PM
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).
: Re: Python help
: twomakewar October 16, 2014, 10:07:59 PM
oh so it divides and returns only the remainder of what you input.
: Re: Python help
: HTH October 16, 2014, 10:22:49 PM
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
: Re: Python help
: Architect October 17, 2014, 01:34:49 AM
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.
: Re: Python help
: techb October 17, 2014, 02:17:02 AM
One does not simply... print after you return.

This made me lol.

But yeah, the function will return before the print happens.