Mastering Python Lambda Functions: A Practical Challenge
Lambda functions, also known as anonymous functions, are a concise way to define small, single-expression functions in Python. They are particularly useful for scenarios where you need a simple function for a short period, often as an argument to higher-order functions like map(), filter(), or sorted(). This challenge will test your understanding and ability to effectively use lambda functions.
Problem Description
Your task is to create a series of lambda functions to perform specific operations on lists of numbers. You will be given a list of integers and will need to apply different transformations and filtering logic using lambda functions.
Key Requirements:
- Squaring Numbers: Create a lambda function that takes a single number and returns its square.
- Filtering Even Numbers: Create a lambda function that takes a single number and returns
Trueif it's even,Falseotherwise. - Multiplying by a Factor: Create a lambda function that takes two numbers (a number and a factor) and returns their product. This lambda function will be used with a pre-defined factor.
- Applying Multiple Operations: Use lambda functions in conjunction with built-in Python functions to achieve more complex transformations.
Expected Behavior:
You should provide a Python script that defines and uses these lambda functions. The script should demonstrate their application on sample input lists and produce the expected output.
Edge Cases to Consider:
- Empty input lists.
- Lists containing zero or negative numbers.
Examples
Example 1: Squaring Numbers
Input: numbers = [1, 2, 3, 4, 5]
Output: squared_numbers = [1, 4, 9, 16, 25]
Explanation: The lambda function squares each element in the input list.
Example 2: Filtering Even Numbers
Input: numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output: even_numbers = [2, 4, 6, 8, 10]
Explanation: The lambda function filters out only the even numbers from the list.
Example 3: Multiplying by a Factor
Input:
numbers = [10, 20, 30, 40]
factor = 5
Output: multiplied_numbers = [50, 100, 150, 200]
Explanation: Each number in the list is multiplied by the given factor using a lambda function.
Example 4: Applying Multiple Operations (Filtering and Mapping)
Input: numbers = [-2, -1, 0, 1, 2, 3, 4, 5, 6]
Operation 1: Filter out non-positive numbers.
Operation 2: Square the remaining positive numbers.
Output: transformed_numbers = [1, 4, 9, 16, 25, 36]
Explanation: First, a lambda function filters for positive numbers. Then, another lambda function squares these filtered numbers.
Constraints
- Input lists will contain integers.
- The maximum length of any input list will be 1000.
- The absolute value of any number in the input list will not exceed 10000.
- The factor for multiplication will be an integer between 1 and 10.
- Your solution should be efficient and primarily utilize lambda functions for the core logic.
Notes
- Consider using
map()andfilter()functions in Python to apply your lambda functions to entire lists. - Remember that lambda functions are best suited for simple, one-line expressions. For more complex logic, a standard
deffunction might be more appropriate. - The goal is to demonstrate your understanding of creating and using lambda functions in various contexts.