TODAY’S PROJECT
Function :-
Lambda Functions in Python
• Lambda: A small anonymous function defined with the lambda keyword instead of def.
• Syntax: lambda arguments: expression
• Lambdas are used for short, throwaway functions, especially as arguments to functions like map() and filter().
Pseudo Code for Squaring a Number Using Lambda
- Start
- Define an anonymous function that receives one argument: • Multiply the argument by itself
- Call this function with input 5
- Print the result (should be 25)
- End
map() Function
• Purpose: Applies a function to every element in an iterable (like a list) and returns a map object (iterator).
• Syntax: map(function, iterable)
• Used when you want to transform all elements in a list (or iterable).
Pseudo Code for Doubling Each Number
- Start
- Create a list of numbers Example: numbers =
- For each element in the list: • Multiply the element by 2 • Store the result in a new list
- Print the new list of doubled numbers
- End
Pseudo Code for Finding Cubes
- Start
- Create a list of numbers Example: nums =
- For each element in the list: • Raise the element to the power of 3 • Store the result in a new list
- Print the new list of cubes
- End
filter() Function
• Purpose: Filters items out of an iterable for which the function (predicate) returns False.
• Syntax: filter(function, iterable)
• Used to keep elements that satisfy a condition.
• Example: Filter even numbers
Pseudo Code for Filtering Even Numbers
- Start
- Create a list of numbers (e.g., )
- For each number in the list: • If the number is divisible by 2 (i.e., number % 2 == 0): • Include the number in a new list of even numbers
- Print the list of even numbers
- End
Pseudo Code for Filtering Words with More Than 5 Letters
1.Start
2.Create a list of words (e.g., ["apple", "banana", "cherry", "kiwi"])
3.For each word in the list:
• If the length of the word is greater than 5:
• Include the word in a new list of long words
4.Print the list of long words
5.End
Top comments (0)