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 a Positive or Negative value, then it returns False otherwise, True.
The syntax of the isnan in this Programming Language is as shown below.
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 this 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
Within the following Python code, let me check this 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. Please refer to the Python isfinite and Python isinf() functions from the list of available Python Math methods.
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))
