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
The float() constructor can create positive and negative infinity.
Published: 1 week ago
🌐
Reddit
reddit.com › r/python › float('inf') is bad practice
r/Python on Reddit: float('inf') is bad practice
April 15, 2024 -

I don't know why float('inf') was chosen as the way the language accesses infinity since this is using a magic string. Why couldn't it just be float.inf? That way magic string is avoided. Feels too basic of a best practice to be simply passed up. Anyone know the reason behind this?

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
🌐
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").
🌐
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().
🌐
Medium
medium.com › @c.fedesna › float-inf-in-python-c942e28340da
Float('inf') in Python - by C. Fedesna
November 21, 2023 - float(‘inf’) in Python is used to represent infinity. Yes, a number to represent infinity… which I keep wondering where it will really be…
🌐
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.
Find elsewhere
🌐
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.
🌐
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.
🌐
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 - There are four primary methods to representing infinite numbers in Python. Let's quickly see them in action: The first method involves utilizing the float() function to represent positive and negative infinity.
🌐
Kodeclik
kodeclik.com › inf-in-python
Infinity in Python: float('inf'), math.inf, and Algorithms
June 16, 2025 - Learn how to represent both positive and negative infinity in Python using float('inf') and math.inf.
🌐
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.
🌐
Scaler
scaler.com › home › topics › how to represent infinity in python?
How to Represent Infinity in Python? - Scaler Topics
May 4, 2023 - Infinity in Python can be represented using the float() method, using the math library, using the Decimal library, and using the numpy library.
🌐
TutorialsPoint
tutorialspoint.com › How-can-I-represent-an-infinite-number-in-Python
How can I represent an infinite number in Python?
November 9, 2022 - Because infinity can be both positive and negative, it can be expressed as a float('inf') and a float('-inf') respectively.
🌐
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. ......
🌐
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?
🌐
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…
🌐
Designcise
designcise.com › web › tutorial › how-to-represent-infinity-in-python
How to Represent "Infinity" in Python? - Designcise
November 21, 2023 - float('inf') and float('-inf') can be used to represent positive and negative infinity respectively: