-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.lambda.py
More file actions
49 lines (35 loc) · 1.18 KB
/
14.lambda.py
File metadata and controls
49 lines (35 loc) · 1.18 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
"""
Lambda
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one expression.
"""
# A lambda function that adds 10 to the number passed
# in as an argument, and print the result:
add = lambda a: a + 10
print(add(5))
# Lambda functions can take any number of arguments:
# A lambda function that multiplies argument a with
# argument b and print the result:
multiply = lambda a, b: a * b
print(multiply(5, 6))
# A lambda function that sums argument a, b, and c
# and print the result:
addition = lambda a, b, c: a + b + c
print(addition(5, 6, 2))
# Why Use Lambda Functions?
# The power of lambda is better shown when you use them as
# an anonymous function inside another function.
# Say you have a function definition that takes one argument,
# and that argument will be multiplied with an unknown number:
def calculateValue(x):
return lambda a: a + x
getTotal = calculateValue(10)
print(getTotal(5))
# create a function definition to make a function that always
# doubles the number you send in
def myfunc(n):
return lambda a: a * n
mydoubler = myfunc(2)
mytripler = myfunc(3)
print(mydoubler(11))
print(mytripler(11))