I have passed the exercise already, but I have seen so many solutions to it in the forum that I´d like to know whether my code is working or just cheating the system, but also if it is the right approach to solving this type of problems or I am just not getting what Python is about. My code is as follows:
def is_int(x): if abs(int(x)) - abs(float(x)) == 0: return True else: return False
Is my code worse than, say, the following one? (from here, which I like a lot)
def is_int(x): y = int(x) if (x-y) == 0: return True else: return False
Also, I have seen people using modulo, as in
def is_int(x): return x % 1 == 0
Which I assume is a “better” way, as it is less lines and therefore seems more efficient. Nevertheless, I don´t get how it works here: why the
True/False statements are not needed when modulo is used?
Sorry for a rather dull question: I am trying to learn what sort of coding is considered “the right way”, besides it plainly working or not.
Thanks a lot for your patience!