-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
75 lines (49 loc) · 1.8 KB
/
Copy pathlambda.py
File metadata and controls
75 lines (49 loc) · 1.8 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
mod2 = lambda num: num % 2
print(mod2(5))
################################################################
# def() vs. lambda()
def square(n):
return n*n
lambda_square = lambda n: n*n
# NOTE: A regular function always returns something,
# even if it's an implicit None.
# A lambda function does not use a 'return' keyword.
# Instead, the expression after the :, is returned.
# A lambda definition is a function and can be assigned to a variable.
# And then you can call the function through that assigned variable.
# Use a lambda function in place of an ordinary function.
print(square(7))
print(lambda_square(7))
(lambda n: print(n*n))(7)
################################################################
def add2(x):
return x + 2
lambda_add2 = lambda x: x + 2
print(add2(6))
print(lambda_add2(6))
print((lambda x: x + 2)(6))
################################################################
def isOdd(x):
return (x%2 == 1)
lambda_isOdd = lambda x: x%2 == 1
print(isOdd(3))
print(lambda_isOdd(3))
print((lambda x: x%2 == 1)(3))
################################################################
def addUp( a, b, c):
return a + b + c
lambda_addUp = lambda a, b, c: a + b + c
print(addUp(4,6,7))
print(lambda_addUp(4,6,7))
print((lambda a,b,c: a+b+c)(4,6,7))
################################################################
# Here, the lambda function has not been called. It is only defined.
# So it will print the function object and its memory location.
# Result: <function <lambda> at 0x7fb30485a9e0>
print(lambda x : x)
# This is the way to pass an argument to the anonymous function
print((lambda x : x)("hey"))
################################################################
x ="macromolecules"
# The argument x is passed through the lambda function, and then printed
(lambda x : print(x))(x)