The builtin math module defines the attribute nan which is used to represent a Not-a-Number (NaN) value.

We get a NaN value when we perform an undefined arithmetic operation such as division by 0, or finding the square root of negative numbers.

ExampleEdit & Run
#import math module
import math

#print the nan value
print(math.nan)
copy
Output:
nan [Finished in 0.010419054888188839s]
Syntax:
math.nan
copy

The math.nan value is a floating point value  which is equivalent to float("nan")

ExampleEdit & Run
import math

print(type(math.nan))
print(float("nan"))
copy
Output:
<class 'float'> nan [Finished in 0.010423439554870129s]

The math.isnan() function returns True if the argument given has a value of nan.

ExampleEdit & Run
import math

print(math.isnan(math.nan))
print(math.isnan(float('nan')))
copy
Output:
True True [Finished in 0.010346978902816772s]

Some  things to note

  1. Two NaN values are never equal to each other.
  2. Any operation with a NaN value always returns NaN
  3. There is no negative NaN.
ExampleEdit & Run
import math

print(math.nan == math.nan)
print(float('nan') == float('nan'))
print(math.nan + 7)
print(-math.nan)
copy
Output:
False False nan nan [Finished in 0.010281362570822239s]