forked from tecladocode/python-refresher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
46 lines (31 loc) · 1021 Bytes
/
code.py
File metadata and controls
46 lines (31 loc) · 1021 Bytes
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
def add(x, y):
return x + y
print(add(5, 7))
# -- Written as a lambda --
add = lambda x, y: x + y
print(add(5, 7))
# Four parts
# lambda
# parameters
# :
# return value
# Lambdas are meant to be short functions, often used without giving them a name.
# For example, in conjunction with built-in function map
# map applies the function to all values in the sequence
def double(x):
return x * 2
sequence = [1, 3, 5, 9]
doubled = [
double(x) for x in sequence
] # Put the result of double(x) in a new list, for each of the values in `sequence`
doubled = map(double, sequence)
print(list(doubled))
# -- Written as a lambda --
sequence = [1, 3, 5, 9]
doubled = map(lambda x: x * 2, sequence)
print(list(doubled))
# -- Important to remember --
# Lambdas are just functions without a name.
# They are used to return a value calculated from its parameters.
# Almost always single-line, so don't do anything complicated in them.
# Very often better to just define a function and give it a proper name.