Python math.isnan() is a built-in method that checks whether a given input is a valid number. The method determines whether the given parameter is a valid number. For example, if the given number x as a parameter is a valid Python number (Positive or Negative), the isnan() function returns False.
Syntax
math.isnan(x)
The math.isnan() function takes only one parameter x, which is any valid data type in Python.
Return Value
The isnan() function returns two types of value:
True: If the given parameter is not a number
False: If the given parameter is a number
Example 1
# Importing math library
import math
# Checking working of isnan()
# When the number is integer
print(math.isnan(15))
# When the number is float
print(math.isnan(14.55))
# When the number is negative
print(math.isnan(-34))
# When the number is not finite
print(math.isnan(float('nan')))
Output
False
False
False
True
In this program, we imported the math library, and then we checked the output of the isnan() function using various inputs.
We have checked the output for integer, float, and negative numbers, respectively; for all these cases, the output is False. Then we checked the output for nan; here, the output is True because nan is not a number.
Example 2
import math
# checking isnan() values
print(math.isnan(math.pi))
print(math.isnan(math.e))
Output
False
False
That’s it.