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))
1def calculateSquare(n):
2 return n*n
3
4
5numbers = (1, 2, 3, 4)
6result = map(calculateSquare, numbers)
7print(result)
8
9# converting map object to set
10numbersSquare = list(result)
11print(numbersSquare)
1# for the map function we give a function (action to act), a data action to act on that data.
2
3# without map
4def multiply_by2(li):
5 new_list = []
6 for number in li:
7 new_list.append(number*2)
8 return new_list
9
10
11print(multiply_by2([1, 2, 3]))
12
13
14# using map function
15def multiply_by_2(item):
16 return item*2
17
18
19print(list(map(multiply_by_2, [1, 2, 3])))
20
21# function that returns usernames in all uppercase letters.
22usernames = ["RN_Tejas", "RN_Tejasprogrammer", "Rajendra Naik", "yooo", "Brainnnni", "Ouuuuut"]
23
24
25def no_lower(item):
26 return item.upper()
27
28
29print(list(map(no_lower, usernames)))
30
31# squares function
32list_squares = list(range(1, 101))
33print(list_squares)
34
35
36def square(number):
37 return number**2
38
39
40square_list = list(map(square, list_squares))
41
42
43def square_root(number):
44 return number // number
45
46
47print(list(map(square_root, square_list)))
48
49
50def no_upper(string):
51 return string.lower()
52
53
54print(list(map(no_upper, 'TEJAS')))
1# Add two lists using map and lambda
2
3numbers1 = [1, 2, 3]
4numbers2 = [4, 5, 6]
5
6result = map(lambda x, y: x + y, numbers1, numbers2,axis=1)
7print(list(result))
8