What is math.isfinite() in Python?

The math.isfinite() function in Python returns True if the value is finite; otherwise, it returns False. In other words, this function is used to check if the number is finite or not.

Figure 1 shows the visual representation of the math.isfinite() function.

Figure 1: Visual representation of math.isfinite() function

The math module is required to use this function.

Syntax

math.isfinite(number)

Parameter

This function requires the number as a parameter.

Return value

This function returns True if the value is finite; otherwise, it returns False.

Example

The following example shows how to use math.isfinite() in Python.

import math
#number
print ("math.isfinite(4):", math.isfinite(4))
print ("math.isfinite(3.5):", math.isfinite(3.5))
print ("math.isfinite(-2.6):", math.isfinite(-2.6))
print ("math.isfinite(0):", math.isfinite(0))
#nan
print ("math.isfinite(nan):", math.isfinite(float("nan")))
#infinity
print ("math.isfinite(inf):", math.isfinite(float("inf")))
print ("math.isfinite(-inf):", math.isfinite(float("-inf")))

Free Resources