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

1. Check if value in x is math.nan

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)

Output

isnan(x) : True

2. Check if integer value 8 is math.nan

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)

Output

isnan(x) : False

Summary

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