In Python, you can do:

test = float("inf")

In Python 3.5, you can do:

import math
test = math.inf

And then:

test > 1
test > 10000
test > x

Will always be true. Unless of course, as pointed out, x is also infinity or "nan" ("not a number").

Additionally (Python 2.x ONLY), in a comparison to Ellipsis, float(inf) is lesser, e.g:

float('inf') < Ellipsis

would return true.

Answer from WilHall on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-infinity
Python infinity (inf) - GeeksforGeeks
The math module provides math.inf as a constant for positive infinity.
Published: 1 week ago
🌐
W3Schools
w3schools.com › python › ref_math_inf.asp
Python math.inf Constant
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... # Import math Library import math # Print the positive infinity print (math.inf) # Print the negative infinity print (-math.inf) Try it Yourself »
People also ask

How do I represent infinity in Python?
Use math.inf or float('inf') for positive infinity and -math.inf or float('-inf') for negative infinity.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python infinity: math.inf, isinf(), and nan
Python Infinity: math.inf, isinf(), and NaN
How do I check whether a Python value is infinity?
Call math.isinf(value). Use math.isfinite(value) when you want to accept only ordinary finite numbers.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python infinity: math.inf, isinf(), and nan
Python Infinity: math.inf, isinf(), and NaN
What is the difference between infinity and NaN?
Infinity represents an unbounded positive or negative value, while NaN represents an undefined numeric result. Test them with isinf() and isnan() respectively.
🌐
pythonpool.com
pythonpool.com › home › tutorials › python infinity: math.inf, isinf(), and nan
Python Infinity: math.inf, isinf(), and NaN
🌐
CodeGym
codegym.cc › java blog › learning python › infinity in python: mastering the infinite
Infinity in Python: Mastering the Infinite
August 13, 2024 - # Comparing positive and negative infinity print(positive_infinity > negative_infinity) # Output: True print(positive_infinity == positive_infinity) # Output: True print(negative_infinity == negative_infinity) # Output: True · In these examples, Python behaves exactly as you’d expect, adhering to the rules of mathematics.
🌐
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.
🌐
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 - In Python, infinity is termed an undefined ("indeterminate" is a better word) value that can be positive or negative. In this brief guide, we'll walk you through representing and using infinity in your Python programs.
🌐
iO Flood
ioflood.com › blog › python-infinity
Python Infinity: Syntax, Uses, and Examples
np.where Python Function | Quick Tips for Numpy · Python Sleep Methods | How to Make Code Pause or Wait · Pip Install Requirements.txt | Python Package Manager · Python Programming Guide · Wordpress Website Speedup Guide · Kubernetes and Docker Guide · Python Syntax Cheat Sheet ·
Find elsewhere
🌐
FavTutor
favtutor.com › blogs › infinity-python
Infinity in Python (float('inf'), math.inf) | FavTutor
July 21, 2026 - Learn how to represent infinity in Python with float('inf') and math.inf, write negative infinity, check values with isinf(), and use inf in searches.
🌐
Python Pool
pythonpool.com › home › tutorials › python infinity: math.inf, isinf(), and nan
Python Infinity: math.inf, isinf(), and NaN
July 13, 2026 - Use infinity when the code genuinely needs an unbounded sentinel. Good examples include shortest-path initialization, minimum and maximum searches, open-ended limits, and temporary placeholders in optimization problems.
🌐
AskPython
askpython.com › python › examples › infinity-in-python
Infinity in Python - Set a Python Variable Value to Infinity - AskPython
October 19, 2020 - The most simple way to set a variable to positive or negative infinity without using any modules is by using float.
🌐
Medium
medium.com › @ryan_forrester_ › infinity-in-python-complete-guide-ad7f108adf15
Infinity in Python: Complete Guide | by ryan | Medium
October 24, 2024 - inf = float('inf') ninf = float('-inf') # Basic comparisons print(inf > 999999999999999) # Output: True print(ninf < -999999999999999) # Output: True # Infinity with itself print(inf == inf) # Output: True print(inf > inf) # Output: False print(inf ...
🌐
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…
🌐
Note.nkmk.me
note.nkmk.me › home › python
Infinity (inf) in Python | note.nkmk.me
August 11, 2023 - You can create inf by specifying the string 'inf' or 'infinity' to float().
🌐
Tambena Consulting
tambenaconsulting.com › home › infinity in python: what you need to know
Infinity in Python: What You Need to Know
November 25, 2024 - Infinity values are especially useful when working with mathematical algorithms where comparisons against extreme values are necessary. For example, you can use positive and negative infinity Python to set starting points in algorithms like finding maximum or minimum values in a list.
🌐
TraceDynamics
tracedynamics.com › python-infinity
Python Infinity: Power of the Boundless within Your Code
December 11, 2023 - In the vast and ever-expanding realm of Python, the concept of infinity holds a unique and captivating position. Unlike the finite limitations of earthly numbers, “infinity” represents the boundless extent, a concept that both excites and mystifies programmers.
🌐
Python.org
discuss.python.org › ideas
Integer infinity - Ideas - Discussions on Python.org
December 11, 2023 - You could accomplish this by annotating retries: int | float and passing retries=float("inf") or retries=math.inf, but it would be nice if you could just pass int("inf") and not need to change the type annotation.
🌐
AlgoCademy
algocademy.com › blog › python-infinity-exploring-the-concept-and-its-applications
Python Infinity: Exploring the Concept and Its Applications – AlgoCademy Blog
The language provides built-in constants and functions to work with infinite values. Python offers multiple ways to represent infinity:
🌐
Medium
koshurai.medium.com › infinity-in-python-156b55ae0c54
Infinity in Python. In Python, the value float("inf")… | by KoshurAI | Medium
December 31, 2022 - For example: x = float("inf") y = 1 print(x > y) # True print(x + y) # inf print(x - y) # inf print(x * y) # inf print(x / y) # inf · If you need to perform arithmetic operations with infinity, you can use the float("inf") and float("-inf") ...
🌐
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
>>> inf - inf Traceback (most recent call last): ... TypeError: unsupported operand type(s) for -: 'Infinity' and 'Infinity' >>> -inf + inf Traceback (most recent call last): ... TypeError: unsupported operand type(s) for +: 'Infinity' and ...
Author: kvesteri