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

Riumu Kuraku cyresse at gmail.com
Fri Oct 15 13:10:31 CEST 2004


>From "Python docs"

The % (modulo) operator yields the remainder from the division of the
first argument by the second. The numeric arguments are first
converted to a common type. A zero right argument raises the
ZeroDivisionError exception. The arguments may be floating point
numbers, e.g., 3.14%0.7 equals 0.34 (since 3.14 equals 4*0.7 + 0.34.)
The modulo operator always yields a result with the same sign as its
second operand (or zero); the absolute value of the result is strictly
smaller than the absolute value of the second operand5.1.

if a number divides by 2, and yields no remainder, it's even.

Be cool and write your own function for this...

def OddorEven(x):
a=0
a=x % 2
if a ==0:
    return "Even"
else:
    eturn "Odd"




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