Lambda Functions - Programs

Lambda Functions – Programs

Lambda Functions – Programs

Program 1: Simple Lambda Function for Addition

# Lambda function for adding two numbers
add = lambda x, y: x + y

# Example usage
result = add(5, 3)
print("Addition Result:", result)

Expected Output:

Addition Result: 8

Program 2: Lambda Function for Multiplication

# Lambda function for multiplying two numbers
multiply = lambda x, y: x * y

# Example usage
result = multiply(4, 7)
print("Multiplication Result:", result)

Expected Output:

Multiplication Result: 28

Program 3: Lambda Function with Conditional Logic (Even or Odd Check)

# Lambda function to check if a number is even or odd
check_even_odd = lambda x: "Even" if x % 2 == 0 else "Odd"

# Example usage
print("10 is:", check_even_odd(10))
print("15 is:", check_even_odd(15))

Expected Output:

10 is: Even
15 is: Odd

Program 4: Lambda Function for Calculating Square of a Number

# Lambda function for squaring a number
square = lambda x: x ** 2

# Example usage
print("Square of 6:", square(6))

Expected Output:

Square of 6: 36

Program 5: Lambda Function for Filtering Even Numbers from a List

# List of numbers
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Using lambda function with filter to get even numbers
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print("Even Numbers:", even_numbers)

Expected Output:

Even Numbers: [2, 4, 6, 8, 10]

See also  Type Casting Programs (5 Programs)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top