What is a Python lambda function?
Answer
A lambda function is a small, anonymous single-expression function defined with the lambda keyword. Syntax: lambda arguments: expression. Example: square = lambda x: x**2; square(5) returns 25. Lambdas are commonly used as quick inline callbacks: sorted(people, key=lambda p: p["age"]), filter(lambda x: x > 0, numbers), map(lambda x: x*2, numbers). Limitations: lambdas can only contain a single expression (no statements, no assignments, no docstrings). For anything non-trivial, use a regular def function instead — it is more readable and testable. In Python, lambdas are most appropriate for simple, one-line operations as arguments to higher-order functions.
Previous
What is the difference between is and == in Python?
Next
What is *args and **kwargs in Python?