Lambda Functions
Lambda functions are small anonymous functions defined with the lambda
keyword.
Syntax
The syntax of a lambda function is: lambda arguments: expression
.
square = lambda x: x ** 2
print(square(5)) # Output: 25
Using Lambda with map
You can use lambda functions with functions like map
.
numbers = [1, 2, 3, 4]
squared_numbers = list(map(lambda x: x ** 2, numbers))
print(squared_numbers) # Output: [1, 4, 9, 16]