Python Practice › If-Else Programs › Program 26
Check if a value is a float
Write a Python program to check whether a given value is a float.
Input
12.6, 12
Output
12.6 is a float 12 is not a float
Approach
- Use
isinstance(value, float).
Solution
for value in [12.6, 12]: if isinstance(value, float): print(value, "is a float") else: print(value, "is not a float")Output
12.6 is a float 12 is not a float
Line by line
Line 2
12 is an int, even though it equals 12.0 numerically.
Learn the concept
New to this topic? Read the lesson, then come back.