Python Lambda

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

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.

Course Video

YouTube Reference :