Open In App

Check If Value Is Int or Float in Python

Last Updated : 01 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
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 do this efficiently.

Using type() Function

type() function returns the type of the object you pass to it. It’s a quick and easy way to inspect whether a value is an int or a float.

Python
a = 14
b = 2.3

print(type(a))
print(type(b))

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

Explanation: type() function returns the data type of the variable. Here, a is an integer and b is a float, so the respective types are printed.

Using isinstance() Function

isinstance() function checks if an object is an instance of a given data type. It returns True or False.

Python
a = 654
b = 566.8

print(isinstance(a, int))    
print(isinstance(a, float))  
print(isinstance(b, int))    
print(isinstance(b, float))  

Output
True
False
False
True

Explanation: Here, we check each variable a and b against both int and float. a is correctly identified as an integer and b as a float.

Using isdigit() Function

isdigit() method is used to check if all characters in a string are digits. It's commonly used for string inputs.

Python
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

Explanation: Although '345.5' represents a number, the presence of the decimal point (.) causes isdigit() to return False. This is because isdigit() expects only digits (0–9) in the string, without any non-digit characters.

Related Articles


Next Article

Similar Reads