# How to test if a variable has a natural number?

Anand Pillai pythonguy at Hotpop.com
Thu Jun 12 13:50:12 CEST 2003

```How about this...

from types import *

def natural(num):
if num>0 and type(num) is IntType:
return 1
else:
return 0

Cheers,

Anand Pillai

Jp Calderone <exarkun at intarweb.us> wrote in message news:<mailman.1055375241.9496.python-list at python.org>...
> On Wed, Jun 11, 2003 at 10:53:24PM +0000, Jack Daniel wrote:
> > Martin P<martin_p at despammed.com> wrote:
> >
> > >
> > > how can I find out if the number of a variable
> > > is a natural number or not?
> > >
> >
> > I'll asume that by "find out if the number of a variable
> > is a natural number or not" you are asking how to determine whether the
> > number a varible is set to is a Natural Number (ie in the set of  whole
> > positive numbers; a positive integer).
> >
> > If this is the case here we go:
> >
> > def natural(x):
> >         if x >= 1 & x % 1 == 0:
>
>   The preferred way to spell this is with "and", not "&".  The semantics are
> slightly different, and the operation you're really looking for is boolean
> and, not bitwise and.
>
> >                 print "Varible is set to a NATURAL"
> >         else:
> >                 print "Varible is NOT set to a natural"
> >
> > run this lil snippet with the name of your varible as arg x
> >
>
>   resisting-the-urge-to-point-out-english-mispellings'ly y'rs,
>
>   Jp

```