Python isnan

The Python isnan function is one of the 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.

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

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 method against NaN (not a number)

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

In this Mathematical function example, we are using the infinity, math.inf, not a number values as the method 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