[Tutor] meaning of % in: if n % x == 0:
Joel Goldstick
joel.goldstick at gmail.com
Wed Aug 31 22:15:21 CEST 2011
On Wed, Aug 31, 2011 at 3:58 PM, Hugo Arts <hugo.yoshi at gmail.com> wrote:
> On Wed, Aug 31, 2011 at 9:35 PM, Lisi <lisi.reisz at gmail.com> wrote:
> > ?? If either n or x or both were 0, and % were the same thing as *, the
> > statement would be true, but from the context I don't think that % does
> mean
> > the same as *, because * appears very soon after in the same fragment of
> > code.
> >
> > Lisi
> >
> > I'm sorry I am making such heavy weather of all this. Blame 2 things:
> anno
> > domini and an exaggerated (obsessive) need to understand language.
>
> % is the remainder operator:
>
>
> http://docs.python.org/library/stdtypes.html#numeric-types-int-float-long-complex
>
> it is how much remains after you perform an integer division, e.g. 23
> % 5 == 3, since 20 / 5 == 4 and then you are left with 3.
>
> n % y == 0 if n is divisible by y. This is useful in factoring prime
> numbers, and in the case of y == 2, for checking if the number n is
> even.
>
> Hugo
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
Here is a little example that shows how using the 'mod' or 'modulus'
operator can select only values that are divisible by 3.
>>> for i in range(16):
... if not i % 3:
... print i
...
0
3
6
9
12
15
--
Joel Goldstick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110831/3d7eaff5/attachment-0001.html>
More information about the Tutor
mailing list