Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Compute the Product of List Elements

Python Compute the Product of List Elements

Document Object Reference Manual Python3 Examples

Define a list of numbers and compute the product of its elements.

For example:


输入 : list1 = [1, 2, 3] 
输出 : 6 
计算:1 * 2 * 3

Example 1

def

multiplyList

(

myList

)

:

     

    result

=

1

   

for

x

in

myList:

         result

=

result * x  

   

return

result  

     

list1

=

[

1

,

2

,

3

]

 

list2

=

[

3

,

2

,

4

]

print

(

multiplyList

(

list1

)

)

print

(

multiplyList

(

list2

)

)

The output of the above example is:

6
24

Document Object Reference Manual Python3 Examples