def is_number(x): ''' Takes a word and checks if Number (Integer or Float). ''' try: # only integers and float converts safely num = float(x) return True except ValueError as e: # not convertable to float return False
Here is what the above code is Doing:
1. We define a function called is_number.
2. We try to convert the word to a float.
3. If the word can be converted to a float, we return True.
4. If the word cannot be converted to a float, we return False.