
Hi there! A lambda function in Python is a neat way to create small, anonymous (unnamed) functions without the usual def statement.
A lambda function is essentially a compact way to write simple functions. It's useful when you need a function for a short period, or you want to use the function just once in your program.
Here's a quick example to illustrate a lambda function:
# Regular function to add 10 to a number
def add_ten(x):
return x + 10
# Lambda function to add 10 to a number
add_ten_lambda = lambda x: x + 10
print(add_ten(5)) # Output: 15
print(add_ten_lambda(5)) # Output: 15
In the above example, add_ten_lambda is a lambda function that performs the same task as the regular function add_ten, but it’s more concise.
Here’s the basic syntax of a lambda function:
lambda argument(s): expression
Use it whenever you need a quick, throwaway function. It fits well for short-term use like sorting, filtering, or operations involving built-in functions.
Hope this helps! Feel free to ask more if needed!
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer