Python math.isfinite() – Check if Number is not Infinity or NaN

Python math.isfinite()

math.isfinite(x) function returns True if x is neither infinity nor a NaN, and False otherwise.

Syntax

The syntax to call isfinite() function is

math.isfinite(x)

where

ParameterRequiredDescription
xYesA numeric value.

Examples

In the following example, we take value of 8 in variable x, and check if this value is finite or not using math.isfinite() function.

Python Program

import math

x = 8
result = math.isfinite(x)
print('isfinite(x) :', result)
Run

Output

isfinite(x) : True

Let us take positive infinity in x, and check if x is finite or not programmatically using math.isfinite() function.

Python Program

import math

x = math.inf
result = math.isfinite(x)
print('isfinite(x) :', result)
Run

Output

isfinite(x) : False

Check if x is finite, with x = nan.

Python Program

import math

x = math.nan
result = math.isfinite(x)
print('isfinite(x) :', result)
Run

Output

isfinite(x) : False

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.isfinite() function.