[Tutor] meaning of % in: if n % x == 0:
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
> > 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:
> > domini and an exaggerated (obsessive) need to understand language.
> % is the remainder operator:
> 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
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
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
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor