[Tutor] Find out if a number is even or not

Lloyd Hugh Allen chandrakirti at gmail.com
Sat Oct 16 05:09:45 CEST 2004


There's also the bitwise & operation

def isEvenQ(num):
    return not (num & 1)

don't know if it's actually faster though.

On Fri, 15 Oct 2004 12:00:18 +0100, nick at javacat.f2s.com
<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