isinf(n)
import math
print(math.isinf(2)) # False
print(math.isinf(-2)) # False
print(math.isinf(0.0)) # False
print(math.isinf(0/1)) # False
print(math.isinf(0.0/1)) # False
Note : 0.0 is considered finite.
import math
print(math.isinf(float('inf'))) # True
print(math.isinf(float('-inf'))) # True
print(math.isinf(float('nan'))) # False
Infinity in Calculations:print(math.isinf(1e308 * 2)) # Output: True
Infinity from Division:print(math.isinf(1 / 0.0)) # Output: True
Using with Lists:numbers = [10, float('inf'), float('-inf'), 42]
inf_values = [n for n in numbers if math.isinf(n)]
print(inf_values) # Output: [inf, -inf]
import math
print(math.isinf(float('inf'))) # True
print(math.isnan(float('nan'))) # True
import math
try:
result = 1.0 / 0.0
except ZeroDivisionError:
result = float('inf')
if math.isinf(result):
print("Result is infinity")
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.