Using Lambda Expression you can create an Anonymous Function in Python. Normal Python functions are created by using the def keyword. But when you want to declare a function anonymously use Python Lambda Expression.
The expression is executed and the result is returned:
lambda arguments : expression
Example Lambda Expression Python
Lambda functions are anonymous or nameless (a function without a name).
You use these functions when:
- To perform a simple operation and,
- Use this function just once.
Simple example code:
Single argument adds, and returns the result:
res = lambda a: a + 10 print(res(10))
Lambda functions with Multiple arguments
res = lambda a, b: a * b print(res(5, 7))
Python Lambda Function with List Comprehension
res = [lambda x=x: x * 10 for x in range(1, 11)] for table in res: print(table(), end=', ')
10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
Do comment if you have any doubts or suggestions no this Pytho lambda tutorial.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.