Here's a code snippet that checks if a number is an integer or not, it works for both Python 2 and Python 3.
import sys if sys.version < '3': integer_types = (int, long,) else: integer_types = (int,) isinstance(yourNumber, integer_types) # returns True if it an integer isinstance(yourNumber, float) # returns True if it a float
Note that Python 2 has both int and long types, while Python 3 has only int type. Source
If you want to check if your number is a number with a float representing int , do this
(isinstance(yourNumber, float) and (yourNumber).is_integer()) # True for 3.0
If you don't need to distinguish between int and float, and you agree with any of them, then ninjagecko's answer is the way to go
import numbers isinstance(yourNumber, numbers.Real)
Agostino Apr 29 '15 at 18:10 2015-04-29 18:10
source share