Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

reduce with default

from functools import reduce

print(reduce(lambda x,y: x+y, [], 0))      # 0
print(reduce(lambda x,y: x+y, [1, 2], 0))  # 3
print(reduce(lambda x,y: x+y, [3, 4], 2))  # 9

print( reduce(lambda x,y: x*y, [1, 2], 0) )  # 0
print( reduce(lambda x,y: x*y, [2, 3], 1) )  # 6
print( reduce(lambda x,y: x*y, [], 0) )      # 0
print( reduce(lambda x,y: x*y, [], 1) )      # 1

Output:

0
18
0
3
0
6
0