How To Multiply A Range In Python

This article will share with you how to multiply a range in Python. To do that, you can use the prod(), reduce(), accumulate() functions, or the loop in Python. Keep reading for detailed instructions.

Multiply A Range In Python

We will show you how to multiply a range in Python in 5 ways:

• Using the prod() function in the math package.
• Using the prod() function in the numpy package.
• Using the reduce() function in the functools package.
• Using the accumulate() function in the itertools package.
• Using the loop.

Use the prod() function in the math package

The prod() function will return the multiplication of a set of values. So, you can use this function to multiply a range in Python.

Look at the example below.

import math

def multiplyARange(start, end):
# Create a range by the range() function, which contains the value from start to end - 1.
myRange = range(start, end)

# Multiple a range.
return math.prod(myRange)

# Try this function.
print('The multiplication of the range: {}'.format(
multiplyARange(start=5, end=10)))

Output

The multiplication of the range: 15120

Use the prod() function in the numpy package

Like the prod() function in the math package, the prod() function in the numpy package will return the multiplication of a set of values. So, you can use this function to multiply a range in Python.

Look at the example below.

import numpy

def multiplyARange(start, end):
# Create a range by the range() function, which contains the value from start to end - 1.
myRange = range(start, end)

# Multiple a range.
return numpy.prod(myRange)

# Try this function.
print('The multiplication of the range: {}'.format(multiplyARange(start= 5, end= 10)))

Output

The multiplication of the range: 15120

Use the reduce() function in the functools package

You can use the reduce() function in the functools package to multiply a range. But you have to create the function, and the reduce() function will apply it to the range.

Look at the example below.

from functools import reduce

def multiplyARange(start, end):
# Create a range by the range() function, which contains the value from start to end - 1.
myRange = range(start, end)

# Multiple a range.
return reduce((lambda x, y: x * y), myRange)

# Try this function.
print('The multiplication of the range: {}'.format(multiplyARange(start= 5, end= 10)))

Output

The multiplication of the range: 15120

Use the accumulate() function in the itertools package

When you use the accumulate() function in the itertools package, you must convert it to the list to get the result of a set of multiplication of a range. So, the last element will be the result that we need to find.

Look at the example below.

from itertools import accumulate

def multiplyARange(start, end):
# Create a range by the range() function, which contains the value from start to end - 1.
myRange = range(start, end)

# Multiple a range.
result = list(accumulate(myRange, (lambda x, y: x * y)))

# The last element of the list is the result.
return result[-1]

# Try this function.
print('The multiplication of the range: {}'.format(multiplyARange(start= 5, end= 10)))

Output

The multiplication of the range: 15120

Use the loop

A simple way that can help you multiply a range is using the loop and multiplying all elements.

Look at the example below.

def multiplyARange(start, end):
# Create a range by the range() function, which contains the value from start to end - 1.
myRange = range(start, end)

# Multiple a range.
result = 1
for element in myRange:
result*= element
return result

# Try this function.
print('The multiplication of the range: {}'.format(multiplyARange(start= 5, end= 10)))

Output

The multiplication of the range: 15120

Summary

We have shown you how to multiply a range in Python. In my opinion, you should use the prod() function because of its performance. However, if you want to customize your problem, you can use the reduce() function or accumulate() function and create your function in it. We hope this tutorial is helpful to you. Thanks!