can someone explain how can i determine if
an input or a variable is an integer
I saw somebody using this "abs()"
and importing some modules that I dont recognized in this exercise
I'm probably sure that they didn't taught me how to do this ...

Help ..

Replace this line with your code.


You might change your input to an integer and test if (your input-int(your input))==0



I had this Idea of solving this problem, but I dont think its the best solution and its more complicated


if type(x) == int:

return True

but what if I want to check if it has a decimal point,
if you could help me with that that would be great!


If you want to check if it has a decimal point, there are three cases:

  • It doesn't have a decimal point, therefore it's a pure integer

  • It has a decimal point and the decimal part is all 0s

  • It has a decimal point and the decimal part is different from 0

So, you could do that:

def is_int(x):
if type(x) == int:
return True
print "True integer"
elif x-int(x)==0:
return True
print "Integer with a decimal point"
return False


My solution is pleasantly short. I tested whether a number (2) to the power of the input (x) is completely divisible by that number (2), as in it leaves no remainders and returns 0, as all integer powers will do this eg. ( 2 ** 3 = 2 x 2 x 2 ) and all decimals powers will not. I then used abs() so it works for negative inputs (eg -6) otherwise it would turn it to a fraction. Then I got the error that it doesn't work for zero, of course because 2** 0 = 1, so I just said if x is zero its also a integer. Hope that helps, I don't know of any fancy modules so I just thought to prove it mathematically instead, it also shows a use of using the abs() command. Here's my code:

def is_int(x):
if 2 ** abs(x) % 2 == 0 or x == 0:
return True
else: return False


you can use "round" an inbuilt function...


print "True integer"
return True

is better than

return True
print "True integer"


I used: if x % 1 == 0:

this works out aswell right? or am I overlooking something? % takes the modulo so it will always have a residue if the value is not an int in my mind.


Oh wow you should be right…looks like I massively over complicated everything. You still need the “or x==0” though.


thank you for your solution,

I’m actually looking for mathematical solutions insteads of built-in functions because
they didn’t taught me that although I know some of them by searching it on stackoverflow…

I’m quite curious what abs() do
good solution though I dont get some of it cuz of abs()
btw thank you gj :’)


what does round function do,

I want to learn it before I use it in my code hope you understand
can you explain it in more detailed way thankyou :slight_smile:


wow exactly as what am I looking for,
your solution is so decent and easy to understand !


this line answered my problem but…
i wonder why did you put int()
when the value of x is already defined as 'x’
can’t I just put x-x == 0:
or it would bring an error?

thank you sir @betajumper84099


It’s something they taught us, I write the things down so I don’t forget them. abs() gives the distance between the number inputted and zero on a number line. So abs(-10) will return 10, as that’s how far away it is from zero. abs(10) returns 10 as well. Same for any number, returns a positive of that. So basically if the number being negative messes up your maths (like mine did) just use abs() to turn it into a positive number.


In my case, what I did is,
I check if the input is integer type(x) == int
then I return True
else if the input is integer but have a decimal point of zero (7.0)
I make the input a integer int(x), then if x - int(x) is equal to 0
then I return True
else False

def is_int(x):
if type(x)==int:
return True
elif x - int(x) == 0:
return True
return False

print is_int(7)
print is_int(7.0)
print is_int(7.2)


def is_int(x):
if type(x)==int or int(x)-x==0:
return True
return False


This worked without any errors


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.