Explain Lambda functions with example
In : BSc IT Subject : Programming in PythonLambda functions in Python are small, anonymous functions defined using the lambda keyword. These functions can have any number of arguments but can only have a single expression. The result of the expression is implicitly returned. Lambda functions are often used in situations where a simple function is needed for a short period and is not reused elsewhere.
Syntax
lambda arguments: expression
Example :
# Regular function to add two numbers
def add(x, y):
return x + y
# Lambda function to add two numbers
add_lambda = lambda x, y: x + y
# Using both functions
result1 = add(5, 3)
result2 = add_lambda(5, 3)
print("Using regular function:", result1) # Output: 8
print("Using lambda function:", result2) # Output: 8