It acts as an unbounded upper value for comparison. This is useful for finding lowest values for something. for example, calculating path route costs when traversing trees.

e.g. Finding the "cheapest" path in a list of options:

>>> lowest_path_cost = float('inf')
>>> # pretend that these were calculated using some worthwhile algorithm
>>> path_costs = [1, 100, 2000000000000, 50]
>>> for path in path_costs:
...   if path < lowest_path_cost:
...     lowest_path_cost = path
...
>>> lowest_path_cost
1

if you didn't have float('Inf') available to you, what value would you use for the initial lowest_path_cost? Would 9999999 be enough -- float('Inf') removes this guesswork.

Answer from user559633 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-infinity
Python infinity (inf) - GeeksforGeeks
Python does not provide an integer type for infinity, but it supports positive and negative infinity through floating-point values and other numeric types. The float() constructor can create positive and negative infinity. ... positive_infinity ...
Published: 1 week ago
Discussions

Number theory discussion regarding floats, ints and NaN/Inf
That’s right. Heh, so NaN is, pretty literally, “not a number”, but infinity is ? Math-wise at least that doesn’t make sense, infinity has a meaning as a notation in the context of limits, but it’s neither a number nor even a value. Python-wise, we already treat nan and the infs as ... More on discuss.python.org
🌐 discuss.python.org
18
1
April 12, 2023
What does stating a variable as float('inf') do?
float('inf') is infinity. You need that because the minimum of a positive number and zero is always zero, so you are finding the minimum value, you are always returning zero. Though this code is way more complicated than needed. Do you see how you could do this without a for loop? More on reddit.com
🌐 r/learnpython
10
3
August 2, 2020
How do I type a floating point infinity literal in python - Stack Overflow
How do I type a floating point infinity literal in python? I have heard inf = float('inf') is non portable. Thus, I have had the following recommended: inf = 1e400 Is either of these standar... More on stackoverflow.com
🌐 stackoverflow.com
What's different between Decimal and Floating point numbers in Python?
Integers (int type) are "whole numbers", without the fractional part, eg. 10. You use this type when the value is always an integer, e.g. a counter. Floating point numbers (float type) can have fractional part, e.g. 10.1234. They are stored using the closest representation in binary notation. Most numbers cannot be represented accurately, so they are slightly off. You use float for most mathematical calculations involving fractional numbers. Decimal numbers (Decimal type in Python) are used to represent floating point numbers accurately, with a defined precision (a defined number of places after decimal point). They are represented with two integer numbers, one for the integer part and one for the fractional part. For example. 10.1234 is stored as (10, 1234). Decimal type is required when fractional numbers must be represented accurately, with defined precision. The most notable example is financial calculations. Using float type you may get a result of a financial operation as 1010.123456$. But money is expressed with at most two decimal places. What does 0.123456$ mean? You can round it to 1010.12$, but then what happens with the remaining 0.003456$? Some "smart" programmers used that to their advantage in the past and they made a lot of money (which they eventually had to give back). So, for money calculations, you should use Decimal type. A good explanation of the Decimal type is in the documentation: https://docs.python.org/3/library/decimal.html More on reddit.com
🌐 r/learnpython
6
6
March 5, 2021
🌐
FavTutor
favtutor.com › blogs › infinity-python
Infinity in Python (float('inf'), math.inf) | FavTutor
July 21, 2026 - In Python, infinity is a float value written as float("inf").
🌐
Medium
medium.com › @c.fedesna › float-inf-in-python-c942e28340da
Float('inf') in Python - by C. Fedesna
November 21, 2023 - >>> print(math.inf) inf >>> print(float('inf')) inf ... Which… it’s weird to think of infinity matching infinity except when arguing with a child about who loves who more.
🌐
W3Schools
w3schools.com › python › ref_math_inf.asp
Python math.inf Constant
# Import math Library import math # Print the positive infinity print (math.inf) # Print the negative infinity print (-math.inf) Try it Yourself » · The math.inf constant returns a floating-point positive infinity.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Infinity (inf) in Python | note.nkmk.me
August 11, 2023 - There are several ways to create inf, such as using the float() function, the math module, or the NumPy library. You can create inf by specifying the string 'inf' or 'infinity' to float().
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › learning python › infinity in python: mastering the infinite
Infinity in Python: Mastering the Infinite
August 13, 2024 - Python provides a straightforward way to represent infinity using the float() function. To represent positive infinity, you simply use: positive_infinity = float('inf') print(positive_infinity) # Output: inf
🌐
Kodeclik
kodeclik.com › inf-in-python
Infinity in Python: float('inf'), math.inf, and Algorithms
June 16, 2025 - In Python, you can represent (positive) infinity using the special floating-point value float('inf'). Since Python 3.5, you can also use math.inf from the standard library's math module.
🌐
Medium
koshurai.medium.com › infinity-in-python-156b55ae0c54
Infinity in Python. In Python, the value float("inf")… | by KoshurAI | Medium
December 31, 2022 - In Python, the value float("inf") represents positive infinity, and float("-inf") represents negative infinity.
🌐
Medium
allwin-raju.medium.com › infinity-in-python-b1d8e03803aa
Infinity in Python. In Python, you can represent infinity… | by Allwin Raju | Medium
February 5, 2025 - In Python, you can represent infinity using float('inf'). It can be used in mathematical operations, comparisons, and algorithms that…
🌐
Python.org
discuss.python.org › python help
Number theory discussion regarding floats, ints and NaN/Inf - Python Help - Discussions on Python.org
April 12, 2023 - That’s right. Heh, so NaN is, pretty literally, “not a number”, but infinity is ? Math-wise at least that doesn’t make sense, infinity has a meaning as a notation in the context of limits, but it’s neither a number nor even a value. Python-wise, we already treat nan and the infs as a special thing, in the floor, ceil, round and int functions.
🌐
Python Central
pythoncentral.io › infinity-in-python-how-to-represent-and-use-infinite-numbers
Infinity In Python: How to Represent (And Use!) Infinite Numbers | Python Central
June 27, 2023 - Let's quickly see them in action: The first method involves utilizing the float() function to represent positive and negative infinity. To represent positive infinity, you can pass 'inf' as the argument, while '-inf' represents negative infinity.
🌐
GitHub
github.com › kvesteri › infinity
GitHub - kvesteri/infinity: All-in-one infinity value for Python. Can be compared to any object. · GitHub
All-in-one infinity value for Python. Can be compared to any object. Python has float('inf') and float('-inf'). However these simply represent floating point infinity values.
Author: kvesteri
🌐
Reddit
reddit.com › r/learnpython › what does stating a variable as float('inf') do?
r/learnpython on Reddit: What does stating a variable as float('inf') do?
August 2, 2020 -

I was doing the following leetcode problem: 121. Best Time to Buy and Sell Stock

https://leetcode.com/problems/best-time-to-buy-and-sell-stock/

My code was as follows:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        profits = []
        min_price = 0
        maxprofit = 0
        for p in prices:
            min_price = min(p, min_price)
            max_price = max(p, min_price)
            profit = max_price - min_price
            profits.append(profit)
            maxprofit = max(profits)
        return maxprofit

However it was spitting out the wrong answer on the leetcode website.

So I was wondering why I was getting it wrong so I did a bit of searching and decided to change min_price = 0 to min_price = float('inf').

After that change, the result was accepted by LeetCode. Can someone please tell me why setting min_price to zero is incorrect and what exactly float('inf') does and means in this context?

Top answer
1 of 2
1
float('inf') is infinity. You need that because the minimum of a positive number and zero is always zero, so you are finding the minimum value, you are always returning zero. Though this code is way more complicated than needed. Do you see how you could do this without a for loop?
2 of 2
1
what exactly float('inf') does Standard floating point implementations have an "infinite" value (and also a negative infinite value), that basically compares larger than any non-infinite floating point number. It's akin to MAXINT for integers that have a fixed number of bits (Python ints have an arbitrary number of bits, and thus no maximum integer value). So, in Python float('inf') returns the floating-point infinite value. But that's irrelevant. If min_price is initialized to zero, and your list of input 'prices' are all greater than zero, then your calculation of the minimum price will always be incorrect. Initializing the value to 'inf' ensures that any other non-infinite value will become the new minimum; it's akin to initializing min_price to None, and then checking everytime through the loop whether it is still None or not, and if it is, setting it to an initial value from the 'prices' list. You minimum value has to be one from the 'prices' list for the algorithm to work, not the initialized value. max_price = max(p, min_price) Using the reasoning I just gave you, think about why this is also wrong. There's more wrong, actually. Why are you storing a list of profits, if you are calculating it on each loop? Generally, if you are making a list, you'd find the maximum profit once, after the loop has completed. BTW, what if the maximum price occurs before the minimum price?
🌐
Codecademy
codecademy.com › docs › python › math module › math.isinf()
Python | Math Module | math.isinf() | Codecademy
September 4, 2024 - Learn the basics of Python 3.13, one of the most powerful, versatile, and in-demand programming languages today. With CertificateWith Certificate · Beginner Friendly.Beginner Friendly24 hours24 hours · math.isinf(x) x: The number to be checked for infinity. The following example demonstrates how to use the math.isinf() function: import math · print(math.isinf(float('inf'))) # Positive infinity ·
🌐
Scaler
scaler.com › home › topics › how to represent infinity in python?
How to Represent Infinity in Python? - Scaler Topics
May 4, 2023 - We can use the float() function in Python to represent the positive infinity and negative infinity. For positive infinity, we need to pass 'inf' to the float() function and for negative infinity, we need to pass '-inf' to the float() function.
🌐
Finxter
blog.finxter.com › home › learn python blog › python infinity
Python Infinity – Be on the Right Side of Change
September 16, 2020 - Summary: Python Infinity is an ... value in a given code. The numerous ways of using Python infinity are: Using float('Inf') and float('-Inf') Using the math module. ......