[Tutor] Find out if a number is even or not
Kent Johnson
kent_johnson at skillsoft.com
Fri Oct 15 13:16:12 CEST 2004
Nick,
You need the % (modulus) operator. a % b gives the remainder when a is
divided by b. So a % 2 == 0 tests for even:
>>> for i in range(10):
... print i, i%2
...
0 0
1 1
2 0
3 1
4 0
5 1
6 0
7 1
8 0
9 1
Kent
At 12:00 PM 10/15/2004 +0100, nick at javacat.f2s.com wrote:
>Hi group,
>
>Im sure this is simple to do, but Ive just been looking through the python
>docs
>and the archive of this mailing list and cant find what Im after.
>
>I just want to know if a number is even or not.
>
>I've had a look at the math package but that doesnt seem to have anything this
>simple in it.
>
>I thought something like
>
> >>> if 10 div 2 == 0 : print 'even'
> >>> else : print 'odd'
>
>but obviously that doesnt work.
>
>Sorry for such a basic question, but I really cant find out how to do this.
>
>Kind regards
>Nick.
>
>
>
>
>
>-------------------------------------------------
>Everyone should have http://www.freedom2surf.net/
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list