Open In App

Check If Value Is Int or Float in Python

Last Updated : 31 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we’ll explore different ways to check if a value is an integer or float in Python.

To Check If Value Is Int Or Float In Python

Below, we provide examples to illustrate how to check if a value is int or float in Python.

  • Using type() function
  • Using isinstance() function
  • Using isdigit() function

Check If Value Is Int Or Float Using type() Function

In Python, you can simply use the built-in type() function to check the data type of any object or variable. Here is an example where we are using the type() function on an integer and a float value. You can see that the type() function returns the class to which an object belongs:

Python3




# integer value
a = 14
 
# float value
b = 2.3
 
print(type(a))
print(type(b))


Output

<class 'int'>
<class 'float'>


Check If Value Is Int Or Float Using Isinstance() Function

The isinstance() function takes two arguments, an object and a data type and returns boolean outputs – True and False depending on whether the given object belongs to the given data type or not. Here is how you can check if a number is an int or float in Python:

Python3




# integer number
num_1 = 654
 
# float number
num_2 = 566.8
 
# ininstance function
res = isinstance(num_1, int
print(res)
 
res = isinstance(num_1, float
print(res)
 
res = isinstance(num_2, int)
print(res)
 
res = isinstance(num_2, float
print(res)


Output

True
False
False
True


You can further use the isinstance() function to add more functionality to your code as follows:

Python3




num = 344
 
# perform multiplication if the number is an integer
if isinstance(num, int):
    print(num*2)
    print("The number is an integer")
 
# perform division if the number is a float
else:
    print(num/2)
    print("The number is a float")


Output

688
The number is an integer


Using the isinstance() function, you can also check if an object is either an int or a float. For doing this, you will have to pass a tuple with both data types to the isinstance() function. What we are doing here is nothing but like using the isinstance() function twice along with the or operator.

Python3




# integer number
num = 234
 
if isinstance(num, (int, float)):  # pass tuple
    print("The number is either an int or a float")
else:
    print("It's might be a string")


Output

The number is either an int or a float


Check If A Value Is Int Or Float Using Isdigit() Function

The isdigit() method is used to check if all the characters of a string are digits or not. The function will return False as an output even if one value in the string is not a digit. Here is how we can use this function to check if a given string is an int or float:

Python3




a = '345.5'
res = a.isdigit()
 
if res == True:
    print("The number is an integer")
else:
    print("The number is a float")


Output

The number is a float


So far so good, but look at the code given below. Here, we have a string that contains one alphabet. When the code runs, the isdigit() function returns False because one of the values is not a digit. And thus, the control goes to the else block and we can see the output says – ‘The number is a float’ when actually it’s not.

Python3




a = '345f23'
res = a.isdigit()
 
if res == True:
    print("The number is an integer")
else:
    print("The number is a float")


Output

The number is a float


Conclusion

In this post, we saw how we can check if a number is an integer or a float in Python. We also saw how we can check if a string is an integer or a float. We can make use of the various functions of Python like isinstance(), int() and isdigit() to work along the way. Note that if you simply want to know the datatype yourself, you can use the type() function but if you want to further add some functionality for the user depending on the data type, then you can use other methods discussed in this post.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads