lambda expression memo.
Anonymous function. It can be stored in a variable and used in other functions.
lamda1.py
#How to write: lamnda[argument]: [Return value]
f = lambda x: x*2
print(f(5))
#=> 10
To use in a dictionary
lamda2.py
value_map = {
1: lambda num: num + 10,
2: lambda num: num + 20,
3: lambda num: num + 30
}
add_type = 2
num = 1
print(value_map[add_type](num))
#=> 21
Recommended Posts