/
using Lambda function
54 lines (33 loc) · 1.23 KB
/
using Lambda function
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
#example1
list1 = [2, 4, 6, 8]
def add_five(number):
return number + 5
new_list = list(map(add_five, list1))
print(new_list)
#using Lambda:
list1 = [2, 4, 6, 8]
new_list = list(map(lambda x: x + 5, list1))
print(new_list)
#we have a list of two weeks paycheck's amounts
#to do some process on them we can use lambda function to avoid defining a lot of functions
#or use for loops to loop through the list
#using "lambda","map" and "filter" functions
paylist=[1000,2200,1500,1700,2100,900]
hourly=list(map(lambda x: x/80 , paylist))
monthly = list(map(lambda x: x*2 , paylist))
salary= list(map(lambda x: x*24 , paylist))
below30k=list(filter(lambda x: x<30000 , salary))
below12=list(filter(lambda x: x<12 , hourly))
print("hourly paylist is : "+str(hourly))
print("monthly paylist is : "+str(monthly))
print("yearly paylist is : "+str(salary))
print("salaries below 30k's list is : "+str(below30k))
print("hourly pay below 12$ list is : "+str(below12))
#sample activity:
Suppose we have list of student’s grades : [5.5,7,8,9.5,2.5,3,10,9,6,5]
Using Lambda,We want to calculate:
- mean(average)
-list of numbers <=4 and numbers >=8
# extension exercises:
1-compute factorial of a number
2-create fibonacci sequence using lambda