How to Work with Infinity in Python

x N, -∞ <x < +∞

∀ x ∈ Z, -∞ < x < +∞

Meaning every natural number (positive integers, i.e., 0, 1, 2, …) is smaller than positive infinity (+∞) and all positive and negative integers (…, -2, -1, 0, 1, 2, …) are bigger than negative infinity (-∞) and smaller than positive infinity.

# positive infinity
p_inf = float("inf")
# negative infinity
n_inf = float("-inf")
# by using the math library (preferred way)
import math
math.isinf(float("-inf")) #OUTPUT:True
math.isinf(float("inf")) #OUTPUT:True
# by comparing to infinity
float("inf") == float("inf") #OUTPUT:True
float("-inf") == float("-inf") #OUTPUT:True

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store