How can I check for NaN values?

You can use the isnan() function provided by the math module to check if a value is NaN.

Here's an example:

import math

x = float('nan')

if math.isnan(x):
  print('x is NaN')

Watch a course Python - The Practical Guide

Alternatively, you can use the math.isnan() function as a standalone function.

x = ''
if math.isnan(x):
  print('x is NaN')

You can also use the math.isnan() function to check for NaN values in a list or array.

import math

arr = [1, 2, float('nan'), 3, 4]

for value in arr:
  if math.isnan(value):
    print('Found a NaN value')

Note that the math.isnan() function is specifically designed to work with float values, and may not work correctly with other types of objects. If you want to check for NaN values in a more general context, you can use the isinstance() function to check if an object is a float and then use the math.isnan() function to check if it is NaN.

if isinstance(value, float) and math.isnan(value):
  print('Found a NaN value')