2018年1月11日 星期四

Python: Compute the product of a list of integers

Python: Compute the product of a list of integers

Python Basic: Exercise-115 with Solution

Write a Python program to compute the product of a list of integers (without using for loop).
Sample Solution:-
from functools import reduce
nums = [10, 20, 30,]
nums_product = reduce( (lambda x, y: x * y), nums)
print("Product of the numbers : ",nums_product)

=========== RESTART: F:/Python_APSC/py-ex-basic-115.py ===========
Product of the numbers :  6000
>>> 
 reduce(lambda x,y: x+y, [47,11,42,13])
113
The following diagram shows the intermediate steps of the calculation: 
Veranschulichung von Reduce

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...