In Python, infinity is represented by the special constant inf, which can be used to represent positive or negative infinity.
It is part of the float type and can be used in comparisons or mathematical operations.
Positive Infinity: float(‘inf’) represents positive infinity.
Negative Infinity: float(‘-inf’) represents negative infinity.
Examples:
positive_inf = float(‘inf’)
negative_inf = float(‘-inf’)
print(positive_inf > 1000) # True
print(negative_inf < -1000) # True
You can also use inf in mathematical expressions, such as division by zero, or in comparison operations:
x = 1 / 0 # This raises ZeroDivisionError
Infinity is useful for algorithms that require sentinel values, such as finding minimum or maximum values in a set of numbers. It also helps when handling large ranges in scientific or financial calculations.