Python isnan

The Python isnan function is one of the Python Mathematical functions used to check whether the given value is a valid number or not. If it is Positive or Negative value, then isnan returns False otherwise, True.

The syntax of the isnan in Python Programming Language is

math.isnan(value)

Python isnan example

It is a simple example of isnan function to check whether zero is a valid number or not.

#Python isnan

import math
 
print(math.isnan(0))
False

Here, we are using the isnan function on different values of both positive and negative. We have also used math.pi inside this isnan function.

import math
 
print(math.isnan(1))
 
print(math.isnan(0.00))
 
print(math.isnan(-2))
 
print(math.isnan(-4.0002))
 
print(math.isnan(math.pi))
False
False
False
False
False

Let me check this Python function against NaN (not a number)

import math
 
print(math.isnan(float('NaN')))

isnan and NaN output

True

In this isnan Mathematical function example, we are using the infinity, math.inf, not a number values as the function arguments.

import math
 
print(math.isnan(float('NaN')))
 
print(math.isnan(float('inf')))
 
print(math.isnan(float('-inf')))
 
print(math.isnan(math.inf))
 
print(math.isnan(-math.inf))
Python isnan Function 4