Here's a piece of code that checks whether 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's an integer isinstance(yourNumber, float) # returns True if it's a floatNotice that Python 2 has both types int and long, while Python 3 has only type int. Source.
If you want to check whether your number is a float that represents an int, do this
(isinstance(yourNumber, float) and (yourNumber).is_integer()) # True for 3.0If you don't need to distinguish between int and float, and are ok with either, then ninjagecko's answer is the way to go
import numbers isinstance(yourNumber, numbers.Real)Check if a number is an int or float in Python #
Use the isinstance() function to check if a number is an int or float, e.g. if isinstance(my_num, int):. The isinstance function will return True if the passed in object is an instance of the provided class (int or float).
Copied!
my_num = 1357 if isinstance(my_num, int): print('number is int') if isinstance(my_num, float): print('number is float') # ----------------------------------- # ✅ checks if number is either int or float if isinstance(my_num, (int, float)): print('Number is either int or float')We used the isinstance() function to check if a number is an int or a float.
The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.
Copied!
print(isinstance(357, int)) # 👉️ True print(isinstance(357, float)) # 👉️ False print(isinstance(3.14, float)) # 👉️ True print(isinstance(3.14, int)) # 👉️ FalseThe isinstance() function correctly returns whether the passed in object is an instance or a subclass of the provided class.
However, there is an edge case - booleans are also an instance of integers.
Copied!
print(isinstance(True, int)) # 👉️ True print(isinstance(False, int)) # 👉️ TrueThis is because the bool class is a subclass of int.
If you need to check if a number is either int or float, pass a tuple containing the int and float classes in the call to the isinstance() function.
Copied!
my_num = 1357 if isinstance(my_num, (int, float)): # 👇️ this runs print('Number is either int or float')The second argument the isinstance function takes can be a class or a tuple containing multiple classes.
The call to the function above checks if the passed in object is either an int or a float.
Using a tuple in the call to the function is equivalent to using two calls with the OR operator.
Copied!
my_num = 1357 if isinstance(my_num, int) or isinstance(my_num, float): print('Number is either int or float')If you just want to print the type of the number, use the type() class.
Copied!
my_num = 1357 print(type(my_num)) # 👉️ <class 'int'> my_num_2 = 3.14 print(type(my_num_2)) # 👉️ <class 'float'>The type class returns the type of an object.
Most commonly the return value is the same as accessing the __class__ attribute on the object.