Define an Infinite Number in Python

You can define an infinite number in Python as follows,

``infinite_number = float('inf')``

Let's print it out.

``print(infinite_number)``
Output:
``inf``

float('inf') provides a positive intifinite number. In the same way, we can define a negative infinite number as float("-inf)

Example:

``````>>> infinite_number = float('inf')
>>> print("Positive Infinite Number:",infinite_number)
Positive Infinite Number: inf
>>>
>>> if(1000 < infinite_number):
...   print(f"{infinite_number} is greater than 1000")
...
inf is greater than 1000
>>> negative_infinite_number = float('-inf')
>>> print("Negative Infinite Number:",infinite_number)
Negative Infinite Number: inf
>>>
>>> if(1000 > negative_infinite_number):
...   print(f"1000 is greater than {negative_infinite_number}")
...
1000 is greater than -inf
>>> ``````
-

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.