Author Topic: Python help  (Read 870 times)

0 Members and 1 Guest are viewing this topic.

Offline twomakewar

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
Python help
« on: 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:



Code: [Select]
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:

Code: [Select]
if number % 3 == 0:
- I have taken the liberty of reformatting your post. Please use ['code'] tags next time.
« Last Edit: October 16, 2014, 01:56:00 pm by Phage »

Offline Fur

  • Knight
  • **
  • Posts: 216
  • Cookies: 34
    • View Profile
Re: Python help
« Reply #1 on: 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
« Last Edit: October 16, 2014, 10:12:38 am by Fur »

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Python help
« Reply #2 on: October 16, 2014, 10:23:14 am »
If number % 3 == 0:
    print("this number is divisible by 3 because there is no remainder")
~Factionwars

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Python help
« Reply #3 on: October 16, 2014, 12:25:25 pm »
One does not simply... print after you return.

Offline twomakewar

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
Re: Python help
« Reply #4 on: October 16, 2014, 07:51:58 pm »
but why use this % sign.

Offline Phage

  • VIP
  • Overlord
  • *
  • Posts: 1280
  • Cookies: 120
    • View Profile
Re: Python help
« Reply #5 on: 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:

Quote
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).
"Ruby devs do, in fact, get all the girls. No girl wants a python, but EVERY girl wants rubies" - connection

"It always takes longer than you expect, even when you take into account Hofstadter’s Law."

Offline twomakewar

  • NULL
  • Posts: 3
  • Cookies: 0
    • View Profile
Re: Python help
« Reply #6 on: October 16, 2014, 10:07:59 pm »
oh so it divides and returns only the remainder of what you input.

Offline HTH

  • Official EZ Slut
  • Administrator
  • Knight
  • *
  • Posts: 395
  • Cookies: 158
  • EZ Titan
    • View Profile
Re: Python help
« Reply #7 on: 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
<ande> HTH is love, HTH is life
<TurboBorland> hth is the only person on this server I can say would successfully spitefuck peoples women

Offline Architect

  • Sir
  • ***
  • Posts: 428
  • Cookies: 56
  • STFU
    • View Profile
    • Rootd IRC
Re: Python help
« Reply #8 on: 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.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Python help
« Reply #9 on: 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.
>>>import this
-----------------------------