Assignment No:06
- To understand the basics about python inbuilt functions like map, filter and lambda.
- To learn how to write and execute recursive and nested function.
- Upon completing this lab exercise, the learner will be able to
- Able to identify how and when to use map function.
- Able to identify how and when to use filter function.
- Understand the use of lambda function and its implementation.
- Implementation of recursive function and nested function.
Theory:
1. What is recursive function?
Why Do We Need Recursive Functions?
Recursive functions are especially useful when solving problems that can be broken down into smaller, similar sub-problems. Common use cases include:
- Problems with a natural recursive structure, such as tree traversal, factorial calculation, Fibonacci sequence, or solving mazes.
- Code simplicity: Recursion can make code simpler and more elegant for problems like calculating permutations, combinations, or solving mathematical series.
How Recursive Functions Work
- Base Case: Stops the recursion when a certain condition is met.
- Recursive Case: The function calls itself with a smaller input, working toward the base case.
Example: Factorial Calculation
The factorial of a number n
is defined as:
For example:
In recursive terms:
Where the base case is
Program:
def factorial(n):
# Base case
if n == 0 or n == 1:
return 1
# Recursive case
return n * factorial (n - 1)
# Using the recursive function
print (factorial (5)) # Output: 120
How it works:
factorial (5)
callsfactorial (4)
.factorial (4)
callsfactorial (3)
,- factorial (3) calls factorial (2),
- factorial (2) calls factorial (1)
- When
factorial (1)
is called, it returns 1 (base case). - The values are then multiplied as the recursion "unwinds":
- factorial (1) returns 1 as an output.
- which gets multiply with n= 2 so 2 * factorial (2-1) => 2 * factorial (1) => 2 * 1 => 2
- That means factorial (2) returns 2 as an output which gets multiply with, n = 3 so 3 * factorial (3-1) => 3 * factorial (2) => 3 * 2 => 6
- That means factorial (3) returns 6 as an output which gets multiply with, n = 4 so 4 * factorial (4-1) => 4 * factorial (3) => 4 * 6 => 24
- That means factorial (4) returns 24 as an output which gets multiply with, n = 5 so 5 * factorial (5-1) => 5 * factorial (4) => 5 * 24 => 120
- So, finally we get factorial of 5 is 120.
- .
- Why do we require recursive function explain with example? already answer given above.
- What is mean by nested function explain with example?
- What is map () function and why do we require map () function explain with example?
- What is filter () function and why do we require filter () function explain with example?
- What is lambda () function and why do we require lambda () function explain with example?
- To understand the basics about python Classes concept
- To learn how to write and execute python object-oriented program.
- Upon completing this lab exercise, the learner will be able to
- Write a basic python program using class.
- Able to implement various features of object-oriented programming.
- Understand the memory management of objects.
- Why do we require Inheritance?
- With example explain the concepts of multiple inheritance.
- Explain with example multiple exceptions?
- What is assert statement?
- Explain with example how to raise an exception?
- What is IO Stream and buffers?
- Explain how to create file in writing mode? also how to write content inside the file and how to read those and print?
- When to use seek () and tell () method explain with example.?
- Explain the difference between readline() and readlines() method?
- Explain with example method overriding concept?