0% found this document useful (0 votes)
18 views

Python Ca2

python report writing

Uploaded by

BIKRAM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python Ca2

python report writing

Uploaded by

BIKRAM
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Recursion in Python

DEPARTMENT OF COMPUTER APPLICATION


CONTAI COLLEGE OF LEARNING & MANAGEMENT
SCIENCE

NAME: Bikram Adak


UNIVERSITY ROLL NO.: 34001222017
STREAM: BCA
SEMESTER: 4th
PAPER: Python Programming
PAPER CODE: BCAC403
SESSION:2022-2025
ABSTRACT
Recursion in Python programming can be understood abstractly as a
powerful problem-solving technique rooted in the concept of self-
reference and self-similarity. It embodies the idea of breaking down
complex problems into simpler, more manageable instances of the
same problem. At its core, recursion relies on two essential
components: a base case and a recursive case. The base case serves
as the terminating condition, halting the recursive process and
providing a result without further recursion. On the other hand, the
recursive case involves invoking the function again with a modified
version of the original problem, gradually converging towards the
base case. This abstract perspective emphasizes the elegance and
universality of recursion, allowing programmers to tackle a wide
range of problems by leveraging the inherent structure and
symmetry present in the underlying tasks. Recursion facilitates a
modular and intuitive approach to problem-solving, promoting code
readability and maintainability. Moreover, recursion fosters a deeper
understanding of computational processes and encourages a
mindset conducive to algorithmic thinking and problem
decomposition. By embracing recursion, Python programmers can
harness its expressive power to devise elegant solutions to complex
problems across various domains.
INTRODUCTION
Python programming is a versatile and high-level language known
for its simplicity and readability. It is widely used in various domains
including web development, data analysis, artificial intelligence, and
scientific computing. Python's extensive standard library and
thriving ecosystem of third-party packages make it a popular choice
for both beginners and experienced developers. With its clean and
concise syntax, Python emphasizes code readability and encourages
a structured and organized approach to programming. Its
interpreted nature allows for rapid prototyping and development,
facilitating iterative and agile development processes. Python
supports multiple programming paradigms including procedural,
object-oriented, and functional programming, offering flexibility and
adaptability to diverse project requirements. Its dynamic typing and
automatic memory management contribute to its ease of use and
reduce development time. Python's community-driven development
model fosters collaboration and innovation, leading to continuous
improvement and expansion of its capabilities. Its cross-platform
compatibility ensures that Python code can run seamlessly on
various operating systems. Overall, Python's simplicity, versatility,
and vibrant community make it an ideal choice for building a wide
range of applications, from simple scripts to complex systems.
LITERATURE REVIEW
DEFINATION:
The process in which a function calls itself directly or indirectly is
called recursion and the corresponding function is called a recursive
function. Using a recursive algorithm, certain problems can be
solved quite easily
ADVANTAGES OF USING RECURSION:
 A complicated function can be split down into smaller sub-
problems utilizing recursion.
 Sequence creation is simpler through recursion than utilizing
any nested iteration.
 Recursive functions render the code look simple and effective.
DISADVANTAGES OF USING RECURSION:
 A lot of memory and time is taken through recursive calls which
makes it expensive for use.
 Recursive functions are challenging to debug.
 The reasoning behind recursion can sometimes be tough to
think through.
SYNTAX:
def func(): <--
|
| (recursive call)
|
func() ----
EXAMPLE:
# Recursive function
def recursive_factorial(n):
if n == 1:
return n
else:
return n * recursive_factorial(n-1)
# user input
num = 6
# check if the input is valid or not
if num < 0:
print("Invalid input ! Please enter a positive number.")
elif num == 0:
print("Factorial of number 0 is 1")
else:
print("Factorial of number", num, "=", recursive_factorial(num))

Output:
Factorial of number 6 = 720

BASE CONDITION:
In the recursive program, the solution to the base case is provided
and the solution to the bigger problem is expressed in terms of
smaller problems.
def fact(n):
if n <= 1:
return 1
else:
return n*fact(n-1)
In the above example, the base case for n < = 1 is defined and the
larger value of a number can be solved by converting to a smaller one
till the base case is reached.

DIRECT RECURSIVE FUNCTION:


When a function calls the same function again, we call it a direct
function. Factorial is an example of direct recursive as shown below:
def factorial(n):
if n == 0: # Base condition
return 1
else:
return n * factorial(n - 1) # Recursive call

# Test the factorial function


number = 5
result = factorial(number)
print(f"The factorial of {number} is:", result)

Output:
The factorial of 5 is: 120
INDIRECT RECURSIVE FUNCTION:
In Indirect recursive functions, two functions mutually call each other
wherein both the functions must have a base case. Let's understand
using a flowchart.

Here, function1 calls function2 which in turn calls function2 which


calls the first function. This can be explained using an example.
Suppose, you have a list of 10 numbers. For every odd number in the
list, you add 1 to the number and for every even number in the list,
you subtract 1. If the number is 1, output should be 2 and similarly, if
the number is 2, output must be 1. You can employ indirect recursive
functions to solve such problems like shown here:
def is_even(n):
if n == 0:
return True
else:
return is_odd(n - 1)
def is_odd(n):
if n == 0:
return False
else:
return is_even(n - 1)
#Test the functions
number = 5
print(f"{number} is even:", is_even(number))
print(f"{number} is odd:", is_odd(number))

Output:
5 is even: False
5 is odd: True
TAIL RECURSION:
Tail recursion is a specific form of recursion where the recursive call is
the last operation performed by the function. In other words, the
result of the recursive call is immediately returned without any
further computation. This type of recursion is particularly interesting
because it can be optimized by some programming languages
(though not by Python due to its lack of proper tail call optimization).
Here's an example of a tail-recursive function to calculate the
factorial of a non-negative integer:
def factorial(n, accumulator=1):
if n == 0:
return accumulator
else:
return factorial(n - 1, accumulator * n)
result = factorial(5)
print("Factorial of 5 is:", result)
Output: Factorial of 5 is: 120
CONCLUSION
Recursion in Python is a powerful technique where a function calls
itself directly or indirectly, allowing elegant solutions to complex
problems. It enables breaking down problems into simpler
subproblems and solving them iteratively, often following a divide-
and-conquer approach. Python supports both direct and indirect
recursion, offering flexibility in problem-solving. While recursion
promotes code readability and modularity, it may lead to stack
overflow errors if not implemented with proper base conditions.
Though Python lacks tail call optimization, recursion remains valuable
for its simplicity and versatility in algorithmic design and problem-
solving. Overall, mastering recursion in Python enhances a
programmer's ability to tackle a wide range of computational
challenges efficiently and elegantly.

REFERENCES
Jenny Bryan, Jim Hester, and STAT545 Teaching Assistants. Happy git and
GitHub for the user. https://happygitwithr.com/, 2021. URL:
https://happygitwithr.com/.

J. D. Hunter. Matplotlib: a 2d graphics environment. Computing in Science &


Engineering, 9(3):90–95, 2007.

Edwin A. Abbott. Flatland. Seeley and Co., 1884.

Hadley Wickham and Jenny Bryan. R Packages. O'Reilly Media, Inc., 2015.

You might also like