[N00B] What's %?

Discussion in 'Python' started by administrata, Feb 10, 2005.

  1. administrata

    administrata Guest

    Hi! it's been about a week learning python!
    I've read 'python programming for the absolute begginer'
    I don't understand about % like...

    107 % 4 = 3
    7 % 3 = 1

    I'm confused with division :/
    Please help me...

    thx 4 reading.
     
    administrata, Feb 10, 2005
    #1
    1. Advertising

  2. On 2005-02-10, administrata <> wrote:

    > I don't understand about % like...
    >
    > 107 % 4 = 3
    > 7 % 3 = 1


    It's the modulus operator. It returns the remainder of integer
    division. As we used to say in second grade:

    4 goes into 107 26 times with 3 left over.

    3 goes into 4 2 times with 1 left over.

    --
    Grant Edwards grante Yow! If I pull this SWITCH
    at I'll be RITA HAYWORTH!! Or
    visi.com a SCIENTOLOGIST!
     
    Grant Edwards, Feb 10, 2005
    #2
    1. Advertising

  3. administrata on 2005-02-10 09:38:41 -0800:

    > Hi! it's been about a week learning python!
    > I've read 'python programming for the absolute begginer'
    > I don't understand about % like...
    >
    > 107 % 4 = 3
    > 7 % 3 = 1
    >
    > I'm confused with division :/


    It's not division; the division operator is '/'. It's the mod
    function, which returns the remainder - for example, 7 divided by 3 is
    2 remainder 1, so 7 % 3 returns 1.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.6 (NetBSD)

    iD8DBQFCC54sAud/2YgchcQRAo0GAJ0al6lQYfEie1YL1Ul9P9tACYuZwACggtGf
    bnBmlm25CgcfT0jgsb5C4As=
    =ktys
    -----END PGP SIGNATURE-----
     
    Alec Berryman, Feb 10, 2005
    #3
  4. On Thu, 10 Feb 2005 09:41:07 -0800 (PST), administrata
    <> wrote:
    > Hi! it's been about a week learning python!
    > I've read 'python programming for the absolute begginer'


    I hope you are enjoying it. ;-_

    > I don't understand about % like...
    >
    > 107 % 4 = 3
    > 7 % 3 = 1


    It;'s modular aritmetic. See
    <http://en.wikipedia.org/wiki/Modular_arithmetic> and
    <http://www.python.org/doc/2.3.4/ref/binary.html>.

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Feb 10, 2005
    #4
  5. administrata

    Jeremy Jones Guest

    administrata wrote:

    >Hi! it's been about a week learning python!
    >I've read 'python programming for the absolute begginer'
    >I don't understand about % like...
    >
    >107 % 4 = 3
    >7 % 3 = 1
    >
    >I'm confused with division :/
    >Please help me...
    >
    >thx 4 reading.
    >
    >

    % is the remainder operator (I think it's also called modulus).

    107 % 4 == 3
    because
    107 / 4 == 26 R3

    and 7 % 3 == 1
    because 7 / 3 == 2 R1


    HTH,

    Jeremy Jones
     
    Jeremy Jones, Feb 10, 2005
    #5
  6. administrata a écrit :
    > Hi! it's been about a week learning python!
    > I've read 'python programming for the absolute begginer'
    > I don't understand about % like...
    >
    > 107 % 4 = 3
    > 7 % 3 = 1


    it's the modulo operator (if you don't remember, the modulo is the
    remaining of the integer division, ie 5 % 2 = 1)

    One of the most commun use is to test wether a number is odd or even:

    any_even_number % 2 == 0
    any_odd_number % 2 == 1


    Note that the % operator is also used for string formating, ie:
    "%d modulo %d = %d" % (5, 2, 1)
    => "5 modulo 2 = 1"

    > Please help me...

    HTH
    Bruno
     
    Bruno Desthuilliers, Feb 10, 2005
    #6
  7. In article <>, administrata wrote:
    > I don't understand about % like...
    >
    > 107 % 4 = 3
    > 7 % 3 = 1
    >


    That's the modulo operation; it returns the remainder, rather than the quotient.

    --
    zoerhoff(AT)sdf.lonestar.org
    kristian.zoerhoff(AT)gmail.com
     
    Kristian M Zoerhoff, Feb 10, 2005
    #7
  8. administrata

    Peter Hansen Guest

    Grant Edwards wrote:
    > It's the modulus operator. It returns the remainder of integer
    > division. As we used to say in second grade:
    >
    > 4 goes into 107 26 times with 3 left over.
    >
    > 3 goes into 4 2 times with 1 left over.


    How long were you stuck in second grade, Grant? <grin>

    -Peter

    P.S. You're correct, for large values of four.... ;-)
     
    Peter Hansen, Feb 10, 2005
    #8
  9. On 2005-02-10, Peter Hansen <> wrote:
    > Grant Edwards wrote:
    >> It's the modulus operator. It returns the remainder of integer
    >> division. As we used to say in second grade:
    >>
    >> 4 goes into 107 26 times with 3 left over.
    >>
    >> 3 goes into 4 2 times with 1 left over.

    >
    > How long were you stuck in second grade, Grant? <grin>


    What? 50% wasn't a passing grade when you were in 2nd grade?

    > P.S. You're correct, for large values of four.... ;-)


    and for small values of 3.

    --
    Grant Edwards grante Yow! I'm a GENIUS! I
    at want to dispute sentence
    visi.com structure with SUSAN
    SONTAG!!
     
    Grant Edwards, Feb 10, 2005
    #9
  10. administrata

    Harlin Guest

    In the mode of anticipating another question... I get these all the
    time at work of all places! You'd think IT workers would know the
    answer to these...

    What good is the modulus operator? What would I ever need it for?

    * A quick way of testing whether an integer is even and odd
    * For that matter, a quick way of testing whether a the variable is a
    factor of any other arbitrary number.
    * In some programs (a weight control program I worked on comes to mind)
    it's necessary to get a remainder so that you can get the results of a
    leftover evenly divisible number.

    Regards,

    Harlin


    administrata wrote:
    > Hi! it's been about a week learning python!
    > I've read 'python programming for the absolute begginer'
    > I don't understand about % like...
    >
    > 107 % 4 = 3
    > 7 % 3 = 1
    >
    > I'm confused with division :/
    > Please help me...
    >
    > thx 4 reading.
     
    Harlin, Feb 10, 2005
    #10
  11. administrata

    Jeff Shannon Guest

    Harlin wrote:

    > What good is the modulus operator? What would I ever need it for?
    >
    > * A quick way of testing whether an integer is even and odd
    > * For that matter, a quick way of testing whether a the variable is a
    > factor of any other arbitrary number.
    > * In some programs (a weight control program I worked on comes to mind)
    > it's necessary to get a remainder so that you can get the results of a
    > leftover evenly divisible number.


    Also, it's a good way to ensure that some number is in a specified
    range, and "wraps around" to the beginning if it goes out of that
    range. For a quick & cheesy example, let's say we want to count time
    for music:

    import time
    def beats = ['one', 'two', 'three', 'four']

    n = 0
    while True:
    print beats[n]
    n = (n+1) % 4
    time.sleep(0.5)

    By using '% 4', I ensure that n is always in the interval [0...4)
    (including 0 but not including 4).

    Modulus is useful for all sorts of periodic behavior.

    Jeff Shannon
    Technician/Programmer
    Credit International
     
    Jeff Shannon, Feb 10, 2005
    #11
  12. On 10 Feb 2005 17:45:37 GMT, Grant Edwards <> declaimed
    the following in comp.lang.python:

    > division. As we used to say in second grade:
    >
    > 4 goes into 107 26 times with 3 left over.
    >
    > 3 goes into 4 2 times with 1 left over.


    And the teacher gave you a grade of 50...

    4 % 3 => 1 and 1 remainder

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Feb 11, 2005
    #12
  13. administrata

    administrata Guest

    (administrata) wrote in message news:<>...
    > Hi! it's been about a week learning python!
    > I've read 'python programming for the absolute begginer'
    > I don't understand about % like...
    >
    > 107 % 4 = 3
    > 7 % 3 = 1
    >
    > I'm confused with division :/
    > Please help me...
    >
    > thx 4 reading.


    sry, i don't know much about maths

    What is % used for?

    such as?
     
    administrata, Feb 11, 2005
    #13
  14. administrata

    Robert Kern Guest

    administrata wrote:

    > sry, i don't know much about maths
    >
    > What is % used for?
    >
    > such as?


    Among many other things, you can use it to test whether one integer
    evenly divides another integer.

    For example, to test if a number is odd:

    def isodd(x):
    return bool(x % 2)

    --
    Robert Kern


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
     
    Robert Kern, Feb 11, 2005
    #14
  15. administrata

    Enoch Guest

    administrata wrote:
    > Hi! it's been about a week learning python!
    > I've read 'python programming for the absolute begginer'
    > I don't understand about % like...
    >
    > 107 % 4 = 3
    > 7 % 3 = 1
    >
    > I'm confused with division :/
    > Please help me...
    >
    > thx 4 reading.


    % means modulus, which is simply, the remainder of A divided by B
    so:

    7 % 3 = 1

    because only two threes go into seven, leaving 1 remainder. Modulus only
    returns that remainder.

    And 107 % 4 = 3 because 26 4's go into 107 leaving 3 over.

    Make sense?

    Enoch.
     
    Enoch, Feb 11, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Koncept
    Replies:
    3
    Views:
    1,955
    Nick Santos
    Dec 6, 2003
  2. newgenre
    Replies:
    1
    Views:
    310
    Curt_C [MVP]
    Apr 14, 2004
  3. newgenre

    n00b needs SQL Server

    newgenre, Apr 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    289
    Curt_C [MVP]
    Apr 15, 2004
  4. newgenre

    n00b Help Setting up MSDE SQL Server

    newgenre, Apr 18, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    1,338
  5. newgenre
    Replies:
    2
    Views:
    935
    Matt Berther
    Apr 19, 2004
Loading...

Share This Page