1# Python program to demonstrate working
2# of map.
3
4# Return double of n
5def addition(n):
6 return n + n
7
8# We double all numbers using map()
9numbers = (1, 2, 3, 4)
10result = map(addition, numbers)
11print(list(result))
12# result [2, 4, 6, 8]
1from functools import reduce
2product = reduce((lambda x, y: x * y), [1, 2, 3, 4])
3
4# Output: 24
5
1product = 1
2list = [1, 2, 3, 4]
3for num in list:
4 product = product * num
5
6# product = 24
7
1items = [1, 2, 3, 4, 5]
2squared = []
3for i in items:
4 squared.append(i**2)
5
1>>> numbers = [1, 2, 3, 4, 5]
2>>> squared = []
3
4>>> for num in numbers:
5... squared.append(num ** 2)
6...
7
8>>> squared
9[1, 4, 9, 16, 25]
10