Velocity Reviews > [N00B] What's %?

# [N00B] What's %?

Guest
Posts: n/a

 02-10-2005
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 :/

Grant Edwards
Guest
Posts: n/a

 02-10-2005
On 2005-02-10, administrata <(E-Mail Removed)> 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!

Alec Berryman
Guest
Posts: n/a

 02-10-2005

> 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-----

Simon Brunning
Guest
Posts: n/a

 02-10-2005
On Thu, 10 Feb 2005 09:41:07 -0800 (PST), administrata
<(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed),
http://www.brunningonline.net/simon/blog/

Jeremy Jones
Guest
Posts: n/a

 02-10-2005

>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 :/
>
>
>

% 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

Bruno Desthuilliers
Guest
Posts: n/a

 02-10-2005
> 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"

HTH
Bruno

Kristian M Zoerhoff
Guest
Posts: n/a

 02-10-2005
In article <(E-Mail Removed) >, 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

Peter Hansen
Guest
Posts: n/a

 02-10-2005
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....

Grant Edwards
Guest
Posts: n/a

 02-10-2005
On 2005-02-10, Peter Hansen <(E-Mail Removed)> 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!!

Harlin
Guest
Posts: n/a

 02-10-2005
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

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

> 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 :/
>