API · Python
Python math.isnan() Method
The math.isnan() is an in-built Python function used to check if a value is a valid number or not.
On this page
Python math.isnan() method is used to check whether the specified number is NaN (not a number) or not.
Syntax
math.isnan(x)
Parameters
x |
The required positive or negative number to check. |
Returns
The return type of this method is a Boolean value. True is returned if x is a NaN (not a number); otherwise, False.
Example Code: Use of the math.isnan() Method
Example Code:
import math
x = 100
value = math.isnan(x)
print(f"Is {x} not a valid number? {value}")
x = math.pi
value = math.isnan(x)
print(f"Is {x} not a valid number? {value}")
x = -98.67
value = math.isnan(x)
print(f"Is {x} not a valid number? {value}")
x = math.inf
value = math.isnan(x)
print(f"Is {x} not a valid number? {value}")
value = math.isnan((float("nan")))
print(f"Is NaN not a valid number? {value}")
Output:
Is 100 not a valid number? False
Is 3.141592653589793 not a valid number? False
Is -98.67 not a valid number? False
Is inf not a valid number? False
Is NaN not a valid number? True
Note that the above code shows how we can use this method.