The Equivalent Of Sum() For Multiplication In Python

The equivalent of sum() for multiplication in Python

To understand about the equivalent of sum() for multiplication in Python, let follow the article below.

The equivalent of sum() for multiplication in Python

Example:

myList = [ 1, 2, 3, 4 ]

Output:

24

Explanation of ideas: 1*2*3*4 = 24

To understand more about the equivalent of sum() for multiplication in Python, I have the following ways:

Use the numpy.prod () function

Syntax:

numpy. prod ( a , axis = None , dtype = None , out = None , keepdims = <no value> , initial = <no value> , where = <no value> )

The numpy.prod () function will return the product of the elements of the iterable object.

Example:

  • Import the numpy module.
  • Initialize a list of integers.
  • Use the numpy.prod() function to multiply the integers in the list.
import numpy

myList = [1, 2, 3, 4]

# Use the numpy.prod() to get the multiplications
result1 = numpy.prod(myList)

print('Multiplication result:', result1)

Output:

Multiplication result: 24

Use the mul() function

Syntax:

mul(a,b)

Parameters:

  • a, b: Two arguments passed to the function.

The mul() function will return the product of the multiplication of the two arguments passed to the function.

Example:

  • Import the operator module.
  • Initialize a list of integers.
  • Use the mul() function to multiply the integers in the list.
from operator import*

myList = [1, 2, 3, 4]
result = 1

for i in myList:
# Use the mul() function of the operator module
	result = mul(i, result)

print('Multiplication result:', result)

Output:

Multiplication result: 24

Use the math.prod() function

Since Python version 3.8, the prod() function is included in the math module, you can use this function to calculate the product of the elements of an iterable object.

Syntax:

math.prod(iterable, start)

Parameters:

  • iterable: Objects such as LIST, TUPLE, or SET need to produce the elements.
  • start: Marks start to calculate elements. The parameter is not required.

The math.prod() function will return the product of the elements of the iterable object.

Example:

  • Import the numpy module.
  • Initialize a list of integers.
  • Use the math.prod() function to multiply the integers in the list.
import math

myList = [1, 2, 3, 4]

# Use the math.prod() to get the multiplications
result1 = math.prod(myList)

print('Multiplication result:', result1)

Output:

Multiplication result: 24

Use traversal by index

Example:

def multiply(myList):
	# Multiply elements one by one
	result = 1
	for i in range(0, len(myList)):
		result = result * myList[i]
	return result
	
myList = [1, 2, 3, 4]
print('Multiplication result:', multiply(myList))

Output:

Multiplication result: 24

In the example above, the multiply function calculates the elements of an iterable object: by implementing a for loop with the variable ‘result’ that stores the result after each element swap inside the iterable object.

Summary

Thank you for taking the time to read our full article. Hope the above information helps you learn more about the equivalent of sum() for multiplication in Python.

Maybe you are interested:

Leave a Reply

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