-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLambda Expression.py
More file actions
146 lines (61 loc) · 1.58 KB
/
Lambda Expression.py
File metadata and controls
146 lines (61 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# lambda expression are known as anonymous functions
# one time use functions
# and never refernccene them again
# In[2]:
def square(num):
return num**2
# In[3]:
my_nums = [1,2,3,4,5]
# In[13]:
# I wanna do mu_nums valus all to square individually what is the possible soluton?
# loop ? why not map?
# # MAP
# In[5]:
map(square , my_nums)
# In[6]:
for item in map(square , my_nums):
print(item)
# In[ ]:
# I want the list back
# list(map(square,my_nums))
# In[10]:
def checkEven(num):
return num % 2 == 0
# In[11]:
my_nums = [1,2,3,4,5,6,7,8,9,10]
# check numbers that even or odd
# In[12]:
list(map(checkEven , my_nums))
# # FILTER
# In[17]:
# Differece between filter and map , map iterate all the values but filter filter the condition
# and get only that list thats meet the filter condition
# In[18]:
list(filter(checkEven , my_nums))
# # Lambda
# In[21]:
# def square_func(num) : return num**2
# replace def to lambda
# arguemtn num and delete the return
# it is anonymus it has no name
# it uses one time
square = lambda num : num**2
square(10)
# In[33]:
# convert this function to lambda expression
# def checkEven(num):
# return num % 2 == 0
checkEven = lambda num : num % 2 == 0
checkEven(2)
checkEven(5)
my_nums = [1,2,3,4,5,6,7,8,9,10]
# check list indivdiual items that either even or odd using map
list(map(checkEven,my_nums))
# In[25]:
# check list indivdiual items that either even or odd using filter
# In[34]:
list(filter(checkEven,my_nums))
# In[ ]: