A lambdafunction in Python is a small, anonymous function defined with the
lambda keyword. It can take any number of arguments but has only one expression, which is implicitly returned.
Key points:
Anonymous (no name).
Usually used for short, simple functions.
Syntax: lambda arguments: expression
Returns the value of the expression.
Example:
# Normal function
def square(x):
return x * x
# Lambda equivalent
square_lambda = lambda x: x * x
print(square(5)) # Output: 25
print(square_lambda(5)) # Output: 25
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
A lambda function in Python is a small, anonymous function defined with the
lambdakeyword. It can take any number of arguments but has only one expression, which is implicitly returned.Key points:
lambda arguments: expressionExample:
Common use case: Sorting with a custom key
Lambda functions are handy for quick functions you don’t need to reuse or name explicitly. Want me to show lambda examples in other languages?