Check if a value is a float

EasyIf-Else Programs · 26 of 64

Write a Python program to check whether a given value is a float.

Advertisement
Input
12.6, 12
Output
12.6 is a float
12 is not a float

Approach

  1. Use isinstance(value, float).

Solution

Python
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.

More if-else programs

← All 64 If-Else Programs

Leave a Comment