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 float
Notice 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.0
If 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)) # 👉️ False
The 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)) # 👉️ True
This 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.
How do you check if a number is a 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 ).
How do you check if a variable is a float?
The is_float() function checks whether a variable is of type float or not. This function returns true (1) if the variable is of type float, otherwise it returns false.
How do you check if a string is a float?
To check if a string is an integer or a float:
Use the str. isdigit() method to check if every character in the string is a digit. If the method returns True , the string is an integer. If the method returns False , the string is a floating-point number.
Is float or int Python?
Numbers in Python exist in two chief forms: integers and floats. As noted in Lesson 02, integers are numbers without a decimal point, whereas floats are numbers with a decimal point. This is am important distinction that you MUST remember, especially when working with data imported from and exported to Excel.
|