Python: is_int


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
return False


if we look at the documentation:

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)


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