Kodeclik Logo

Our Programs

Courses

Learn More

Schedule

Kodeclik Blog

How do you represent infinity in Python?

Infinity is a standard number in many mathematical calculations and useful in many situations. For instance, when implementing algorithms (e.g., Dijkstra's shortest path), you might initialize distances to infinity and later update them with lower values. Thus infinity is useful as an upper bound in calculations.

Note that there is both positive and negative infinity. Think of traveling the number line in either direction. Positive and negative infinities are at two ends of the number line.

Infinity in Python

In Python, you can represent (positive) infinity using the special floating-point value float('inf'). Since Python 3.5, you can also use math.inf from the standard library's math module. Negative infinity is represented similarly using float('-inf').

Representing infinity in Python with float() values

Consider the below code:

# Representing positive and negative infinity
positive_infinity = float('inf')
negative_infinity = float('-inf')

print("Positive Infinity:", positive_infinity)  
print("Negative Infinity:", negative_infinity)  

# Demonstrating arithmetic properties:
print("Positive Infinity + 1:", positive_infinity + 1)  
print("Positive Infinity * 2:", positive_infinity * 2) 

This code snippet demonstrates how Python represents infinity using floating-point numbers and how it behaves in arithmetic operations. The code creates two variables, one for positive infinity by calling float('inf') and another for negative infinity by calling float('-inf'). When these variables are printed, Python outputs inf for the positive infinity and -inf for the negative infinity, clearly indicating their special status as unbounded values.

The snippet then shows that arithmetic operations with infinity follow the expected mathematical rules: adding a finite number (in this case, 1) to positive infinity results in positive infinity, and similarly, multiplying positive infinity by 2 still yields positive infinity.

These properties are particularly useful in scenarios like algorithm initialization for finding minimum values, where you want to start with a value that any real number would be less than, or in various mathematical computations that require a representation of an unbounded limit.

The output will be:

Positive Infinity: inf
Negative Infinity: -inf
Positive Infinity + 1: inf
Positive Infinity * 2: inf

Representing infinity in Python with math.inf()

Consider now this code:

import math

# Representing infinity using math.inf
infinity = math.inf

print("Infinity using math.inf:", infinity)

Note that this code begins by importing the math module, which provides access to various mathematical functions and constants. It then uses the constant math.inf to represent positive infinity, assigning it to the variable named infinity.

When the code executes the print statement, it displays the text "Infinity using math.inf:" followed by the value of infinity, which is shown as inf.

The output will be:

Infinity using math.inf: inf

Thus, Python's math module allows you to work with the concept of infinity, providing a built-in way to represent an unbounded value in your computations.

Using Infinity in an Algorithm

Here’s an example of how you might use infinity to initialize a minimum value when searching through a list:

# A list of numbers
numbers = [4, 1, 3, 7, 2]

# Initialize min_value to positive infinity
min_value = float('inf')

for number in numbers:
    if number < min_value:
        min_value = number

print("The minimum value is:", min_value)  

This code initializes a list of numbers and uses a loop to find the minimum value in the list by initializing a variable `min_value` to positive infinity (`float('inf')`).

The loop iterates through each number in the list, and during each iteration, it checks if the current number is smaller than the current value of `min_value`. If it is, `min_value` is updated to that number.

Since positive infinity is used as the initial value, any finite number in the list will be smaller than it, ensuring that `min_value` eventually becomes the smallest number in the list. After the loop completes, the minimum value (`1` in this case) is printed to the console.

The output will be:

The minimum value is: 1

Checking for Infinity

You can also check whether a number is infinite using the math.isinf() function:

import math

x = float('inf')
print("Is x infinite?", math.isinf(x))  

The output will be:

True

as expected.

So in summary, positive infinity is denoted as float(‘inf’) or math.inf and negative infinity as float(‘-inf’). The primary use cases for infinity in python are initialization in algorithms, bounds for comparisons, representing unbounded quantities, and handling special cases in mathematical computations.

Enjoy this blogpost? Want to learn Python with us? Sign up for 1:1 or small group classes.

Kodeclik sidebar newsletter

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

About

Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.

Copyright @ Kodeclik 2025. All rights reserved.