• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Infinity – How to Use Infinity in Python

Python Infinity – How to Use Infinity in Python

August 12, 2022 Leave a Comment

To use infinity in Python and create variables which represent infinity, there are four different ways.

The easiest way to get infinity in Python is with the float() function.

infinity = float('inf')

print(infinity)

#Output:
inf

Another way you can represent infinity in Python is with the math module.

import math

infinity = math.inf

print(infinity)

#Output:
inf

Another way you can represent infinity in Python is with the decimal module.

import decimal 

infinity = decimal.Decimal("inf")

print(infinity)

#Output:
inf

One final way to represent infinity is with the numpy module.

import numpy as np

infinity = np.inf

print(infinity)

#Output:
inf

Each of these methods create variables which are equal to infinity and they are all have the same representations in Python.

import math
import decimal
import numpy as np

print(float('inf') == math.inf == decimal.Decimal('inf') == np.inf)

#Output:
True 

How to Use Infinity in Python

After you’ve initialized a variable and assigned it a value of infinity with one of the four methods shown above, you can use infinity just like any other number in Python.

For example, you can perform any of the basic arithmetic operations such as addition, subtraction, division or multiplication.

You can also do things such as comparing infinity with other numbers or finding the maximum or minimum of numbers including infinity.

infinity = float('inf')

print(infinity + 100)
print(infinity - 100)
print(infinity * 100)
print(100 / infinity)
print(max(infinity, 100))
print(min(infinity, 100))

#Output:
inf
inf
inf
0.0
inf
100

Using Negative Infinity in Python

If you want to use negative infinity in Python, then you can multiply any of the above variables by -1 and get negative infinity.

For example, if you want to use the float() function to create infinity, then just multiply the returned value by -1 and you’ll get negative infinity.

Below shows a simple example of how you can get negative infinity in Python.

negative_infinity = -float('inf')

print(negative_infinity)

#Output:
-inf

Then after you’ve created a variable representing negative infinity, you can use it just like positive infinity.

Hopefully this article has helped you understand how to create variables representing infinity in Python.

Other Articles You'll Also Like:

  • 1.  Changing Python Turtle Speed with speed() Function
  • 2.  Python divmod() Function – Get Quotient and Remainder After Division
  • 3.  Change Python Turtle Background Color with screensize() Function
  • 4.  Sign Function in Python – Get Sign of Number
  • 5.  Cartesian Product of Two Lists in Python
  • 6.  How to Shutdown Computer with Python
  • 7.  Find First Occurrence in String of Character or Substring in Python
  • 8.  Skip Numbers in Python Range
  • 9.  How to Declare Variable Without Value in Python
  • 10.  pandas idxmin – Find Index of Minimum Value of Series or DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy