In Python, infinity is a float value written as float("inf"). It is larger than every other number, and its negative counterpart float("-inf") is smaller than every other number.
Python offers three ways to get infinity: float("inf"), math.inf, and NumPy's np.inf. This lesson shows each one, how to write negative infinity, how to check for infinite values, and how infinity behaves in arithmetic and comparisons.
What Is Infinity in Python?
Infinity is a special float that compares greater than any finite number, created with float("inf").
# 1. Create infinity
endless = float("inf")
# 2. It beats any finite number
print(endless > 10 ** 100) # Outputs: True
print(endless) # Outputs: inf
3 Ways to Represent Infinity in Python
1) The float("inf") Constructor
Passing the string "inf" or "Infinity" to float() returns positive infinity. No import is needed, so this is the most common form.
ceiling = float("inf")
print(ceiling) # Outputs: inf
print(float("Infinity")) # Outputs: inf
2) The math.inf Constant
The math module exposes infinity as a named constant. It equals float("inf") exactly; the choice is a matter of style.
import math
print(math.inf) # Outputs: inf
print(math.inf == float("inf")) # Outputs: True
3) NumPy's np.inf Constant
NumPy provides np.inf, which is the same float infinity and mixes freely with the other two forms in arrays and calculations.
import numpy as np
readings = np.array([12.5, np.inf, 3.1])
print(readings.max()) # Outputs: inf
Negative Infinity in Python
Negative infinity is written float("-inf"), -math.inf, or -float("inf"). It compares smaller than every other number.
floor = float("-inf")
print(floor < -10 ** 100) # Outputs: True
print(floor) # Outputs: -inf
How to Check If a Value Is Infinite
math.isinf() returns True for positive or negative infinity, and math.isfinite() is its opposite. A direct comparison with == also works.
import math
speed = float("inf")
print(math.isinf(speed)) # Outputs: True
print(math.isfinite(speed)) # Outputs: False
print(speed == float("inf")) # Outputs: True
How Infinity Behaves in Arithmetic
Most arithmetic keeps infinity infinite: adding, multiplying by a positive number, or dividing by a finite number all return inf. Two operations are undefined and return nan (not a number): inf - inf and inf / inf.
forever = float("inf")
print(forever + 1000) # Outputs: inf
print(forever * -2) # Outputs: -inf
print(1 / forever) # Outputs: 0.0
print(forever - forever) # Outputs: nan
Dividing a finite number by infinity returns 0.0, and nan is not equal to anything, including itself.
Is There an Integer Infinity in Python?
No. Infinity exists only as a float; int("inf") raises a ValueError and int(float("inf")) raises an OverflowError. Python ints are unbounded, so no integer infinity is needed, and float infinity still compares correctly against any int.
print(int("inf"))
# ValueError: invalid literal for int() with base 10: 'inf'
When to Use Infinity in Python
1) A Starting Value for Minimums
When scanning for a minimum, starting at float("inf") guarantees the first real value replaces it. Use float("-inf") when scanning for a maximum.
deliveries = [48.5, 12.0, 33.7]
fastest = float("inf")
for time in deliveries:
if time < fastest:
fastest = time
print(fastest) # Outputs: 12.0
2) An Unbounded Limit
Infinity expresses "no limit" in settings such as budgets, timeouts, or search bounds, without inventing a magic number like 999999.
3) Graph Algorithms
Shortest-path algorithms such as Dijkstra initialize every distance to infinity, meaning "not reached yet", and then shrink them.
Examples of Using Infinity in Python
1) The Cheapest Offer Across Stores
offers = {"store_a": 74.99, "store_b": 69.50, "store_c": 82.00}
best_price = float("inf")
best_store = None
for store, price in offers.items():
if price < best_price:
best_price, best_store = price, store
print(best_store, best_price) # Outputs: store_b 69.5
2) A Rate Limit That Can Be Switched Off
max_requests = float("inf")
for request_number in [500, 15000, 2000000]:
print(request_number < max_requests)
# Outputs:
# True
# True
# True
Learn More About Python Infinity
Infinity vs NaN
Infinity is an ordered value: it wins every comparison. nan is unordered: every comparison with it returns False, even nan == nan. Check for it with math.isnan(), never with ==.
float("inf") vs sys.maxsize
sys.maxsize is a large but finite integer (the maximum size of containers). Numbers above it exist and compare above it, so it is not a substitute for infinity in comparisons.
import sys
print(sys.maxsize < sys.maxsize + 1) # Outputs: True
print(float("inf") > sys.maxsize + 1) # Outputs: True
Infinity in Sorting
Infinite values sort like any float: -inf first, inf last. This makes them useful as sentinel keys, for example sorted(items, key=lambda x: x or float("inf")) to push empty values to the end.
Key Takeaways for Infinity in Python
- float("inf") - positive infinity with no import;
float("-inf")is the negative version. - math.inf and np.inf - the same value as named constants.
- Float only - there is no integer infinity;
int("inf")raisesValueError. - Checks -
math.isinf()andmath.isfinite(); usemath.isnan()fornan. - Undefined operations -
inf - infandinf / infreturnnan, not an error.
The most common job for infinity is seeding a minimum or maximum search. Read our lesson on the Python min() and max() functions for the built-in way to do those scans.

By Kusum Jain 