Recursive Functions in Python are tools that solve problems by breaking them down into smaller parts through self-calling.
Table of contents

Python recursive function
Advantages
To solve complex problems, recursive functions are used by breaking them into smaller parts. They can be helpful for tasks such as searching, sorting, and navigating data structures.
Disadvantages
Recursive functions can be powerful, but they need to be used carefully. If not designed well, they can use up a lot of memory and processing power. They can also cause program crashes or go into endless loops if not implemented correctly.
Recursion examples
Factorial recursion
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
result = factorial(5)
print(result)
Output
120
** Process exited - Return Code: 0 **
Press Enter to exit terminal
In this example, the recursive function factorial calculates the factorial of a given number (n). The base case is when n is equal to 0, in which case the function returns 1. Otherwise, it multiplies n with the factorial of(n-1). The function is called with, and the result is printed, giving us the factorial of 5, which is 120.
Python Tips:
—————-
The with statement would be useful whenever there are related operations block on each other.
with expression [as variable]:
with-block
In the above statement, we see that with block would be used within the context of variable opened with the help of with statement.
with open(‘output.txt’, ‘w’) as f:
f.write(‘|Python!’)
In the above example, the file would be closed as soon as the scope of with block gets over the advantage of being not to close the file explicitly using close block.
Python Shortcut
factorial = lambda n: n if n == 1 else n * factorial(n - 1)
print(factorial(6))
Output
720
** Process exited - Return Code: 0 **
Press Enter to exit terminal
The sum of N numbers
Calculation of the sum in Python Again, you convert the recursive calculation formula of the summation into a recursive function:
def sum_of(n):
if n <= 0:
raise ValueError("n must be >= 1")
# recursive termination
if n == 1:
return 1
# recursive descent
return n + sum_of(n - 1)
print(sum_of(10))
Output
55
** Process exited - Return Code: 0 **
Press Enter to exit terminal
Python shortcut
For the calculation of sums, it is possible to use a lambda, but again without error handling and a bit less readable:
sum_of = lambda n: n if n == 1 else n + sum_of(n - 1)
print(sum_of(10))
Output
55
** Process exited - Return Code: 0 **
Press Enter to exit terminal
Conclusion
In this article, we explored two examples of recursive functions in Python. The first example showed how to calculate the factorial of a number using recursion. We defined a base case and a recursive step to compute the factorial.
The second example demonstrated how to calculate the sum of N numbers using recursion. Again, we defined a base case and used recursion to compute the sum.
Python also provides shortcuts for recursive functions using lambda expressions. While they can be concise, it’s important to consider readability and error handling when using them.
In conclusion, recursive functions in Python are a powerful tool for solving complex problems. By breaking problems into smaller parts, we can write elegant and efficient code.







You must be logged in to post a comment.