DEV Community

Cover image for Day 19 of My AI & Data Mastery Journey: From Python to Generative AI
Nitin-bhatt46
Nitin-bhatt46

Posted on

Day 19 of My AI & Data Mastery Journey: From Python to Generative AI

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

  1. Start
  2. Define an anonymous function that receives one argument: • Multiply the argument by itself
  3. Call this function with input 5
  4. Print the result (should be 25)
  5. 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

  1. Start
  2. Create a list of numbers Example: numbers =
  3. For each element in the list: • Multiply the element by 2 • Store the result in a new list
  4. Print the new list of doubled numbers
  5. End

Pseudo Code for Finding Cubes

  1. Start
  2. Create a list of numbers Example: nums =
  3. For each element in the list: • Raise the element to the power of 3 • Store the result in a new list
  4. Print the new list of cubes
  5. 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

  1. Start
  2. Create a list of numbers (e.g., )
  3. 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
  4. Print the list of even numbers
  5. 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)