Lambda Functions
A lambda function is a small, anonymous function written in a single line. It does not require a function name or the def keyword.
Syntax
lambda arguments: expression
Key characteristics:
- No function name
- No def keyword
- Only one expression allowed
- Used for short and simple operations
Normal Function vs Lambda Function
Normal Function
def add(a, b):
return a + b
print(add(4, 5))
Lambda Function
add1 = lambda a, b: a + b
print(add1(2, 3))
Another example:
square = lambda x: x ** 2
print(square(5))
Lambda functions are useful when you need a quick function for a short period of time.
Higher Order Functions
A higher order function is a function that:
- Takes another function as input, or
- Returns a function as output
Common examples include:
- map()
- filter()
- sorted()
map() Function
Applies a function to every element of an iterable.
nums = [1, 2, 3, 4, 5]
result = list(map(lambda x: x * 2, nums))
print(result)
Output:
[2, 4, 6, 8, 10]
filter() Function
Filters elements based on a condition.
nums = [1, 2, 3, 4, 5]
even = list(filter(lambda x: x % 2 == 0, nums))
print(even)
Output:
[2, 4]
sorted() with Lambda
Used for custom sorting logic.
students = [("Piyush", 20), ("Rahul", 18), ("Amar", 24)]
sorted_list = sorted(students, key=lambda x: len(x[0]))
print(sorted_list)
This sorts based on the length of names.
Key Points About Lambda Functions
- Best for short and simple logic
- Can take multiple arguments
- Limited to a single expression
- Not suitable for complex logic
Recursion
Recursion is a technique where a function calls itself to solve a problem.
Rules of Recursion
Every recursive function must have:
-
Base Case
Condition where recursion stops -
Recursive Case
Function calling itself
Without a base case, recursion will lead to infinite calls and crash the program.
Example: Print Numbers
def print_nums(n):
if n > 5: # Base case
return
print(n, end=" ")
print_nums(n + 1)
print_nums(1)
Output:
1 2 3 4 5
Example: Factorial Using Recursion
def factorial(n):
if n == 1:
return 1
return n * factorial(n - 1)
print(factorial(5))
When to Use Recursion
- When a problem can be broken into smaller subproblems
- Tree structures
- Backtracking problems
- Divide and conquer algorithms
When Not to Use Recursion
- When it increases complexity unnecessarily
- Risk of stack overflow
- When a loop provides a simpler solution
Assignment Questions
Basic Level
- Create a lambda function to add two numbers
- Write a lambda function to find square of a number
- Use map() with lambda to multiply all elements of a list by 3
- Use filter() to get all odd numbers from a list
- Sort a list of integers using lambda
Intermediate Level
- Use map() to convert a list of strings to uppercase
- Use filter() to remove negative numbers from a list
- Sort a list of tuples based on the second value
- Write a recursive function to print numbers from 1 to n
- Modify recursion example to print numbers in reverse
Advanced Level
- Write a recursive function to calculate factorial
- Write a recursive function to calculate Fibonacci series
- Create a function using both lambda and map() to square a list
- Implement a recursive function to find sum of digits of a number
- Compare recursion vs loop for factorial and analyze performance
Summary
- Lambda functions provide a concise way to write small functions
- Higher order functions like map(), filter(), and sorted() enhance functional programming
- Recursion is powerful but must be used carefully
- Choosing between recursion and iteration depends on problem complexity
.png)

0 Comments:
Post a Comment