Python Lambda
In Python, a lambda function is a small, anonymous function defined without a name using the lambda keyword. Unlike regular functions defined using the def keyword, lambda functions are typically used for short, simple operations. Lambda functions in Python are useful for writing concise, anonymous functions for short tasks. They are primarily used when you need a simple function temporarily in your code.
Syntax:
lambda parameters: expression
● parameters: The input parameters to the function.
● expression: The single expression to be evaluated and returned by the lambda function.
Example:
add = lambda x, y: x + y
print(add(5, 10))
# Output: 15
Course Video
Course Video English:
Task:
1. Create lambda functions for arithmetic operations like addition, subtraction multiplication and division.
2. Create a lambda function to check the greater of two numbers. Use comparison operator to check greater of two.
3. Use a lambda function to square a number (5).
4. Use a lambda function to filter out even numbers from a list.
list1 = [1,2,3,4,5,6,7,8]
5. Use a lambda function to find the length of a given string. String: ‘Hello World’
6. Write a Python program that uses lambda and list comprehension to:
● Filter out or remove all even numbers from a list.
● Multiply the remaining odd numbers by 3.
Task Video
YouTube Reference :
A lambda function is a small anonymous function created using the lambda keyword.
The symbol is lambda, used to define these functions.
They allow creating concise functions for single-use scenarios.
Yes, especially for quick operations in functions like map, filter, or sorted.
For creating short functions without formally defining them.
Use it when you need a simple function for temporary use.
Example: lambda x: x + 2 adds 2 to the input.
It simplifies code and eliminates the need for named functions in straightforward use cases.
Lambdas are often used in data transformations, e.g., applying operations to a list or column in a DataFrame.
Example: lambda x: x + 2 adds 2 to the input.