[Tutor] meaning of % in: if n % x == 0:

Hugo Arts hugo.yoshi at gmail.com
Wed Aug 31 21:58:44 CEST 2011


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


More information about the Tutor mailing list