How to check if a number is float or not in Python
In this tutorial, we will learn how to check if a number is float or not in Python. We can check this in three different ways.
- type() method
- comparing with “float”
A more precise number with decimal is float number. We also refer to it as a floating-point number. In python, it is very easy to check whether the number is float or not. Here are the few methods. Let’s start with type() in Python.
Check type of variable
num = 34.22 print(type(num))
Comparison with ‘float’
num = 34.22 if(num == float): print('This number is float') else: print('This number is not float')
This number is float
Use isinstance() method to check if a number is float or not in Python
num = 34.22 print(isinstance(num, float)
In the above example, isinstance() is used to check whether the first argument is an object or ‘instance’ of the second argument or not.
You may also read:
- Check whether a variable is defined or not in Python
- Scope of a variable, global and non-local keyword in Python