A lambda function is a small anonymous function defined using the lambda
keyword.
lambda arguments: expression
Here is an example of a simple lambda function:
add = lambda x, y: x + y
print(add(5, 3)) # Output: 8
Lambda functions are often used in places where you need a small function for a short period of time.
Write a lambda function that returns the square of a number and use it in the map()
function to square a list of numbers.
Answers: The syntax is lambda arguments: expression
. Lambda functions are commonly used in functions like map()
, filter()
, and sorted()
.