Home >Backend Development >Python Tutorial >How to Check for NaN (Not a Number) in Python?

How to Check for NaN (Not a Number) in Python?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-07 19:18:15609browse

How to Check for NaN (Not a Number) in Python?

Checking for NaN (Not a Number)

In Python, NaN (not a number) is represented by float('nan'). It's used to represent values that cannot be represented as real numbers. To check if a value is NaN, utilize the math.isnan function.

Example:

import math

x = float('nan')
if math.isnan(x):
    print("x is NaN")
else:
    print("x is not NaN")

Output:

x is NaN

The above is the detailed content of How to Check for NaN (Not a Number) in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn