Python math.isnan()

nelsonboamortesantiago's avatar
Published Sep 4, 2024
Contribute to Docs

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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

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 math
num = 20
print(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 math
nan = 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:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours