What is math.isnan() in python?

The math module in Python provides access to mathematical functions and constants. math.isnan() checks whether a value is a number or not.

Usage

Suppose you want to validate records that specify the trends in inflation. math.isnan() will be used to check if the values entered are numbers or not. This mechanism is used largely in validating systems.

Syntax


math.isnan(x)

Arguments

x: The value to be checked.

This parameter is required.

Return value

The function returns a Boolean value of true if the value is not a number, and false if it is a number.

Code

#import library
import math
#initialize variables
a = 3
b = -5.6
c = math.inf
d = 9932.435
e = +57
f = 0.00004
g = math.nan
#Check whether variables are NaN
print(a, ':', math.isnan(a))
print(b, ':', math.isnan(b))
print(c, ':', math.isnan(c))
print(d, ':', math.isnan(d))
print(e, ':', math.isnan(e))
print(f, ':', math.isnan(f))
print(g, ':', math.isnan(g))