math.isnan()
In Python, the math.isnan()
function returns True
if the given value is NaN (not a number) and False
if the value is a number.
This method belongs to the group of number-theoretic and representation functions in the math module and is used to handle simple (not complex) numbers. Except when explicitly noted otherwise, this function expects a float-type numeric value.
Syntax
math.isnan(x)
x
: The numeric value to be checked.
Example 1
In the example below, math.isnan()
returns False
because the variable num
is a valid numeric value, not NaN:
import mathnum = 20print(math.isnan(num))
The example above results in the following output:
False
Example 2
In the example below, math.isnan()
returns True
because the variable nan
, created using float
, represents a “Not a Number” (NaN) value:
import mathnan = float('nan')print(math.isnan(nan))
The example above results in the following output:
True
Codebyte Example
Run the following code to understand how the math.isnan()
method works:
All contributors
- nelsonboamortesantiago2 total contributions
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.