Python math.isnan() Method

Python math.isnan() Method

  1. Syntax
  2. Parameters
  3. Returns
  4. Example Code: Use of the math.isnan() Method

Python math.isnan() method is used to check whether the specified number is NaN (not a number) or not.

Syntax

math.isnan(x)

Parameters

x The required positive or negative number to check.

Returns

The return type of this method is a Boolean value. True is returned if x is a NaN (not a number); otherwise, False.

Example Code: Use of the math.isnan() Method

Example Code:

import math

x = 100
value= math.isnan(x)
print(f"Is {x} not a valid number? {value}")

x = math.pi
value= math.isnan(x)
print(f"Is {x} not a valid number? {value}")

x = -98.67
value= math.isnan(x)
print(f"Is {x} not a valid number? {value}")

x = math.inf
value= math.isnan(x)
print(f"Is {x} not a valid number? {value}")

value= math.isnan((float("nan")))
print(f"Is NaN not a valid number? {value}")

Output:

Is 100 not a valid number? False
Is 3.141592653589793 not a valid number? False
Is -98.67 not a valid number? False
Is inf not a valid number? False
Is NaN not a valid number? True

Note that the above code shows how we can use this method.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python Math

  • Python math.pow() Method
  • Python Math.erf() Method
  • Python Math.erfc() Method
  • Python Math.expm1() Method
  • Python Math.fabs() Method
  • Python Math.factorial() Method