Python math.isnan() Method

The math.isnan() method is used to check if a value is a NaN (Not a Number).

Syntax

import math 
math.isnan(x)

Parameters

x(required): The value to be checked.

Return Value

Returns boolean value:

  1. True: If the given value is NaN(not a number)
  2. False: If the given value is a number

Visual Representation

Visual Representation of Python math.isnan() Method

Example 1: How to Use math.isnan() Method

import math

print(math.isnan(15)) #positive number
print(math.isnan(-15)) #negative number
print(math.isnan(5.55)) #float number
print(math.isnan(float('nan')))

Output

False
False
False
True

Example 2: Passing math.pi and math.eVisual Representation of Passing math.pi and math.e

import math

print(math.isnan(math.pi))
print(math.isnan(math.e))

Output

False
False

Example 3: Passing positive and negative infinity

Visual Representation of Passing positive and negative infinity

import math 

print(math.isnan(float('inf')))  # Check if positive infinity is NaN
print(math.isnan(float('-inf'))) # Check if negative infinity is NaN

Output

False
False

Related posts

Numpy.isnan()

Python isinfinite()

Numpy.isfinite()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.