menu
Lambda functions are anonymous functions in Python. They allow you to define a small, one-line function without the need for a formal def keyword and a function name.

Example:
Imagine you want to double a number. Here's how you can do it with a regular function:
def double(x):
  """Doubles a number."""
  return x * 2

result = double(5)
print(result)  # Output: 10

This defines a function named double that takes a number (x) as input and returns its double.
Here's how you can achieve the same functionality using a Lambda function:
# Lambda function to double a number
double_lambda = lambda x: x * 2

result = double_lambda(5)
print(result)  # Output: 10

Use Cases:
  • Passing small functions as arguments to other functions.
  • Sorting or filtering data based on a simple criterion.
  • Creating throwaway functions used only once.