Projects/Lambda_funcs.py
2025-09-12 20:37:36 -07:00

24 lines
812 B
Python

# Define a function that takes a function and a value as arguments
def apply_function(func, value):
return func(value)
# Call the function with a lambda function as the first argument
result = apply_function(lambda x: x * x, 25)
print(result)
# Define a function that applies a given function to each element in a list
def apply_to_list(numbers, func):
"""Applies the given function to each element in the numbers list."""
return [func(x) for x in numbers]
# List of numbers
numbers = [1, 2, 3, 4, 5]
# Using a lambda function to add 10 to each number
result_add = apply_to_list(numbers, lambda x: x + 10)
print("Adding 10:", result_add)
# Using a lambda function to multiply each number by 2
result_multiply = apply_to_list(numbers, lambda x: x * 2)
print("Multiplying by 2:", result_multiply)