checking if a number is int

James Logajan JamesL at Lugoj.com
Sat Apr 6 13:52:26 EST 2002


Steingrim Dovland <steingrd at student.matnat.uio.no> wrote:
> Robert Spielmann <varial at gmx.de> writes:
> 
>> is there a way in python to check if a number is integer? My problem
>> is the following: I need to know if sqrt(x) is an integer value or if
>> there is a fractional part. 
> 
> You can do it using the modulo-operator %.  An integer value % 1 
> will return 0 and a non-integer value % 1 will return something
> greater than 0.

Another way is to try something like:

import math

def IsWhole(x):
   return x == math.floor(x)

if IsWhole(math.sqrt(4.0)):
   print "Whole number"



More information about the Python-list mailing list