checking if a number is int

Johannes Gijsbers taradino at softhome.net
Sat Apr 6 15:55:31 EST 2002


logistix wrote:
> Use python's type system:
> 
> >>> import types
> >>> a, b = 1, 1.0
> >>> if type(a) == types.IntType: print "int"
> ...
> int
> >>> if type(b) == types.IntType: print "int"
> ...
> >>>

That's not really a good idea, as math.sqrt() returns a float. I'd say:

>>> import math
>>> def isInt(number):
...     return int(number) == number
...
>>> isInt(math.sqrt(289))
1
>>> isInt(math.sqrt(315))
0

Johannes Gijsbers
--
-- 
will you be my big plaything
my ninja power
my number cruncher



More information about the Python-list mailing list