You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Lambda Functions is also known as Anonymous function bcoz they don't have any name some tym they also called oneline function bcoz they written in oneline of code
3
+
# function returning from function thats where lambda function are used
4
+
5
+
fromfunctoolsimportreduce
6
+
7
+
# def double(x):
8
+
# return x*2
9
+
#
10
+
# def add(x,y):
11
+
# return x+y
12
+
#
13
+
# def pro(x,y,z):
14
+
# return x*y*z
15
+
16
+
17
+
# Lambda function of above functions
18
+
19
+
double=lambdax : x*2
20
+
add=lambdax, y : x+y
21
+
pro=lambdax, y, z : x*y*z
22
+
23
+
print(double(10))
24
+
print(add(10,20))
25
+
print(pro(10,20,30))
26
+
27
+
# map, filter, reduce
28
+
29
+
my_list1= [2,5,8,10,9,3]
30
+
my_list2= [1,4,7,8,5,1]
31
+
32
+
a=map(lambdax : x*2, my_list1)
33
+
print(list(a)) # u must to cast list otherwise it will give wierd hexadecimal instead of list
34
+
35
+
b=map(lambdax, y : x+y, my_list1 , my_list2)
36
+
print(list(b))
37
+
38
+
# filter function gives boolean value and it take function as 1st argument
39
+
40
+
c=filter(lambdax : x%2==0, my_list1)
41
+
print(list(c))
42
+
43
+
d=filter(lambdax : Trueifx>5elseFalse, my_list1) # using if else inside filter
44
+
print(list(d))
45
+
46
+
# to use reduce we have import functool at the top
0 commit comments