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
>>> 
Python Positive and Negative Infinite Number Example

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

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap