- In python programming language lambda function is an anonymous function or a function having no name.
- It becomes a small and restricted function having no more than one line.
- A lambda function can have multiple arguments with one expression, like a normal function.
- In python lambda expressions or lambda forms are utilized to construct anonymous functions.
- Here we use the lambda keyword just as we use def to define normal functions.
- In python every anonymous function we define will have 3 essential parts, they are lambda keyword, parameters, function body.
- This function can have any number of parameters, but it contains only one expression.
- A lambda function is written in a single line of code and can also be invoked immediately.
- It is not exactly an inline function, but it simulates inline functions of C and C++.
Sample Code
[pastacode lang=”python” manual=”%23%20a%20and%20b%20are%20the%20arguments%20and%20a*b%20is%20the%20expression%20which%20gets%20evaluated%20and%20eturned.%20%20%20%20%0Ax%20%3D%20lambda%20a%2Cb%3A%20a*b%20%20%0Aprint(%22mul%20%3D%20%22%2C%20x(20%2C10))%20%20%20%0Amul%20%3D%20%20200%20%20%0A%23the%20function%20table(n)%20prints%20the%20table%20of%20n%20%20%20%20%0Adef%20table(n)%3A%20%20%20%20%0A%20%20%20%20return%20lambda%20a%3Aa*n%20%23%20a%20will%20contain%20the%20iteration%20variable%20i%20and%20a%20multiple%20of%20n%20is%20returned%20at%20each%20function%20call%20%20%20%20%0An%20%3D%20int(input(%22Enter%20the%20number%3A%22))%20%20%20%20%0Ab%20%3D%20table(n)%20%23the%20entered%20number%20is%20passed%20into%20the%20function%20table.%20b%20will%20containa%20lambda%20function%20which%20is%20called%20again%20and%20again%20with%20the%20iteration%20variable%20i%20%20%20%20%0Afor%20i%20in%20range(1%2C11)%3A%20%20%20%20%0A%20%20%20%20print(n%2C%22X%22%2Ci%2C%22%3D%22%2Cb(i))%20%23the%20lambda%20function%20b%20is%20called%20with%20the%20iteration%20variable%20i%0A” message=”” highlight=”” provider=”manual”/]Output

