python – Multiply all numeric elements within an array

Question:

We have for example the array: [1,2,5,7,12,3,9] How could I multiply all the elements of the array without using a for loop?

Answer:

What you need is to make use of numpy, it has a method known as prod which multiplies all the elements within an array.

<< import numpy as np
<< a = [1,2,3,4]
<< print(np.prod(a))
>> 24

I hope my answer has served you, greetings.

Scroll to Top