Python: is_int


#1

I was messing with the code for this section and I got this to work but have no idea why it does. I think my main confusion would be with x == int(x) and why that should be enough code to give me the correct result.

def is_int(x):
if x == int(x):
return True
else:
return False


#2

if we look at the documentation:

https://docs.python.org/2/library/functions.html#int

we read:

If x is floating point, the conversion truncates towards zero.

Which is why your code is working. If we have a non-zero decimal value, then the truncated value (int(x)) won’t equal the original value (x)


#3

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