-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions lambda Decorators.py
128 lines (81 loc) · 2.47 KB
/
Functions lambda Decorators.py
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
'''
- Lambda Function is a just in time function
- Which is very similar to inline function in c++
- anonymous function
Syntax: variableName = lambda arguments: Expression
varName(arguments)
add = lambda a,b: a+b
print(add(5,8))
x = lambda m,n: m>n
print(x(2,3))
################################################################################################
# map() function----> map function can be used to apply and map a function over an iterable
#- find the list of length of each string in the given list
lst1 = ['This', 'is','the','Python','Programming','Language']
#lst2 = list(map(fun,listVariable))
def lislen(l):
ls=[]
for i in l:
ls.append(len(i))
print(ls)
lislen(lst1)
#- find the list of length of each string in the given list by using map() and simple function
def strl(s):
return len(s)
l = strl('Python')
print(l)
lst1 = ['This', 'is','the','Python','Programming','Language']
lst2 = list(map(strl,lst1))
print(lst1)
print(lst2)
lst1 = ['This', 'was','the','Python','Programming','Language','which','is','awesome']
#- find the list of length of each string in the given list by using map() and lambda function
list3 = list(map(lambda s: len(s),lst1))
print(lst1)
print(list3)
# concartenate 2 numbers and generate the third one by using lambda function
num3 = lambda num1,num2: int(str(num1)+str(num2))
res = num3(3,7)
print(res)
print(type(res))
# filter()---> is used to apply a filter function on an iterable
def eve(n):
if n%2==0:
return n
list4 = list(range(1,11))
print(list4)
list5 = list(filter(eve,list4))
print(list5)
list4 = list(range(1,11))
print(list4)
list5 = list(filter(lambda num:num%2==0,list4))
print(list5)
######################################################################
# Decorators----> it is used to add the functionality to an existing function
# Decorator recieves function as an agrument to which it applies additional functionalities
def message(s):
print(s)
print('******************************************************************')
message("Hello World!")
print('******************************************************************')
def demoDecor(fun):
print('*'*(50))
fun()
print('*'*50)
@demoDecor
def message2():
print("Python")
message2()
'''
# age can't be zero
def ageDecor(fun):
def inner(a):
if a < 0:
print("Age can not be zero")
else:
return fun(a)
return inner
@ageDecor
def age(x):
print("Age is",x)
age(-13)