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
Release: 2024-12-07 19:18:15
Original
591 people have browsed it

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")
Copy after login

Output:

x is NaN
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template