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:
Contribute to Docs
- 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.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours