[Tutor] x%2

Steven D'Aprano steve at pearwood.info
Tue Jan 10 03:23:02 CET 2012


emin wrote:
> http://www.youtube.com/watch?src_vid=QaYAOR4Jq2E&feature=iv&annotation_id=annotation_149056&v=M3g1GEkmyrw
> in this tutorial what does mean x%2 ?

x % 2 gives the remainder when you divide x by 2.

> i think: i * 2% = always even number 

What does that mean? i*2% does not work in Python. In Python, % does not mean 
percentage and so you can't multiply by 2%.

I guess you mean

     i % 2 == 0 means i is an even number

and this would be correct. If i % 2 == 0 then i is even. If it equals 1 then i 
is odd.

> but why not 4,6 or 8? but  i * 4(6,8,10,12...)% = always even number too

The remainder i % n will be a number between 0 and n-1. i%2 is useful because 
there are only two possible results, 0 (i is even) or 1 (i is odd).

i%4 is less useful, because there are four possibilities:

0 (i is even)
1 (i is odd)
2 (i is even)
3 (i is odd)

i%10 is less useful still, because there are ten possibilities:

0,2,4,6,8: i is even
1,3,5,7,9: i is odd


> for example: 100 * 2(4,6,8,10,12...)% = 2(4,6,8,10,12...)
> even numbers = {2,4,6,8,10,12...}

I don't understand what you are trying to say here.


> and how pyton understanding 0(even or odd number or it is an exception?)?

0 is always an even number, because it has 0 remainder when you divide by 2.



-- 
Steven


More information about the Tutor mailing list