Lambdas are one line
functions.
They are also known as anonymous functions in some other
languages. You might want to use lambdas when you don’t want to use a function
twice in a program. They are just like normal functions and even behave like
them.
(lambda x, y: x + y)(2, 3) equals to 5
[(lambda x: x*3)(i) for i in range(0,5)] equals to 0, 3, 6
The best way to learn new things is to take a practical approach of the things you want to learn. Here is a fragment of code that demonstrates the use of the lambda statement in Python:
Æ’ python command
print((lambda x, y: x + y)(2, 3))
print([(lambda x: x*3)(i) for i in
range(0,5)])
√ output
5
[0, 3, 6, 9, 12]
Sign up here with your email
ConversionConversion EmoticonEmoticon