How can I check for the NaN value in Python?

Hi. I have used JavaScript’s built-in function isNaN() and I have not tried it in Python yet. But I have a question.

If I have the following code:

x = float('nan')

Then how could I create a code that checks if the code above is NaN?

Please answer.

Thanks!

Best regards,
Pummarin

You can use try except block

number = "nan"
try:
    num = float(number)
except:
    print("Number expected")

Alternatively you can use string functions,

number = "decimal"
# Functions below
number.isnumeric()    #False
number.isdigit()    # False
number.isdecimal()    # False

Learn more from official documentation.
str.isdecimal()
str.isnumeric()