Python math.isnan() – Check if Number is NaN

Python math.isnan()

math.isnan(x) function returns True if x is NaN (Not a Number), and False otherwise.

Syntax

The syntax to call isnan() function is

math.isnan(x)

where

ParameterRequiredDescription
xYesA value.

Examples

Assign x with NaN value, and programmatically check if x is NaN value, using math.isnan().

Python Program

import math

x = math.nan
result = math.isnan(x)
print('isnan(x) :', result)
Run

Output

isnan(x) : True

Assign x with some value, like 8, and programmatically check if x is NaN, using math.isnan().

Python Program

import math

x = 8
result = math.isnan(x)
print('isnan(x) :', result)
Run

Output

isnan(x) : False

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.isnan() function.