"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Check for NaN (Not a Number) in Python?

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

Published on 2024-12-29
Browse:212

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
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3