Hướng dẫn python recursion practice problems with solutions - vấn đề thực hành đệ quy python với giải pháp

Python: Recursion - Exercises, Practice, Solution

Last update on August 19 2022 21:50:47 (UTC/GMT +8 hours)

Recursion [ 11 exercises with solution]

[An editor is available at the bottom of the page to write and execute the scripts.]

1. Write a Python program to calculate the sum of a list of numbers. Go to the editor
Click me to see the sample solution

2. Write a Python program to converting an Integer to a string in any base. Go to the editor
Click me to see the sample solution

3. Write a Python program of recursion list sum. Go to the editor
Test Data: [1, 2, [3,4], [5,6]]
Expected Result: 21
Click me to see the sample solution

4. Write a Python program to get the factorial of a non-negative integer. Go to the editor
Click me to see the sample solution

5. Write a Python program to solve the Fibonacci sequence using recursion. Go to the editor
Click me to see the sample solution

6. Write a Python program to get the sum of a non-negative integer. Go to the editor
Test Data:
sumDigits(345) -> 12
sumDigits(45) -> 9
Click me to see the sample solution

7. Write a Python program to calculate the sum of the positive integers of n+(n-2)+(n-4)... (until n-x =< 0). Go to the editor
Test Data:
sum_series(6) -> 12
sum_series(10) -> 30
Click me to see the sample solution

8. Write a Python program to calculate the harmonic sum of n-1. Go to the editor
Note: The harmonic sum is the sum of reciprocals of the positive integers.
Example :

Hướng dẫn python recursion practice problems with solutions - vấn đề thực hành đệ quy python với giải pháp

Click me to see the sample solution

9. Write a Python program to calculate the geometric sum of n-1. Go to the editor
Note: In mathematics, a geometric series is a series with a constant ratio between successive terms.
Example :

Hướng dẫn python recursion practice problems with solutions - vấn đề thực hành đệ quy python với giải pháp

Click me to see the sample solution

10. Write a Python program to calculate the value of 'a' to the power 'b'. Go to the editor
Test Data :
(power(3,4) -> 81
Click me to see the sample solution

11. Write a Python program to find  the greatest common divisor (gcd) of two integers. Go to the editor
Click me to see the sample solution

Python Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz

Python: Tips of the Day

Unpack variables using the splat operator:

>>> def test(x, y, z):
>>> 	print(x, y, z)  
>>> res = test(*[10, 20, 30]) 
10 20 30
>>> res = test(**{'x': 1, 'y': 2, 'z': 3} )
10 20 30

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

Start Your Coding Journey Now!

11 Recursion Function Examples for Practice (Easiest 😎 to Hardest🤯)

Solve These Problems To Get Expert At Recursion Function

If you are new to Python and struggle to get your hands dirty with Recursive functions, you must try to solve the problems listed in this article. The article is sorted…

Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding :)


In programming, recursion is a technique using a function or an algorithm that calls itself one or more times until a particular condition is met. 

A recursive function is a function that calls itself with a failure condition. It means that there will be one or more function calls within that function definition itself.</p><p><span id="ezoic-pub-ad-placeholder-153" class="ezoic-adpicker-ad"></span>Let’s see how we can implement recursion using Python. In this article, I have provided a few examples of using recursion in Python. Check out these examples, and I hope they will help you get a clear idea about the concept of recursion in programming. Let’s dive right in.</p><h2>Example 1: Finding the factorial of a number</h2><p>If you are familiar with the concept of factorial in mathematics, this will be a simple example of recursion for you to start with. In mathematics, the equation for finding the factorial of a number is as follows:</p><pre class="wp-block-preformatted">n! = n * (n-1)!</pre><p>Given below is a Python program that finds out the factorial of a number by calling a function recursively.</p><pre class="wp-block-code"><code>def fact(n): if(n==1): return n else: return n*(fact(n-1)) num = int(input("Enter a number: ")) if num&lt;0: print("Negative numbers are not allowed.") elif num==0: print("Factorial is: 1") else: print("Factorial is: ",fact(num)) </code></pre><figure class="wp-block-image size-full"><img width="285" height="78" alt="" class="ezlazyload wp-image-8173" ezimgfmt="rs rscb19 src ng ngcb19" data-ezsrc="https://pythonistaplanet.com/wp-content/uploads/2021/11/image.png"></figure><h2>Example 2: Fibonacci Series</h2><p>Fibonacci is a special kind of series in mathematics in which the current term is the sum of the previous two terms. For example, a <a href="https://pythonistaplanet.com/fibonacci-sequence-iterative/" data-type="URL" data-id="https://pythonistaplanet.com/fibonacci-sequence-iterative/" target="_blank" rel="noreferrer noopener">Fibonacci series</a> would look like this:</p><pre class="wp-block-preformatted">0, 1, 1, 2, 3, 5, 8, 13,…</pre><p><span id="ezoic-pub-ad-placeholder-164" class="ezoic-adpicker-ad"></span><span class="ezoic-ad ezoic-at-0 medrectangle-4 medrectangle-4164 adtester-container adtester-container-164" data-ez-name="pythonistaplanet_com-medrectangle-4"><span id="div-gpt-ad-pythonistaplanet_com-medrectangle-4-0" ezaw="300" ezah="250" style="position:relative;z-index:0;display:inline-block;padding:0;min-height:250px;min-width:300px" class="ezoic-ad"><script data-ezscrex="false" data-cfasync="false" style="display:none">if(typeof ez_ad_units!='undefined'){ez_ad_units.push([[300,250],'pythonistaplanet_com-medrectangle-4','ezslot_7',164,'0','0'])};__ez_fad_position('div-gpt-ad-pythonistaplanet_com-medrectangle-4-0');In this series, the next number is found by adding the two numbers before that.

Given below is a Python code that finds out the Fibonacci series till a number n.

def fib(n): if n<=1: return n else: return fib(n-1) + fib(n-2) count = int(input("Enter the limit: ")) if count<=0: print("Enter a number greater than 0") else: for i in range(count): print(fib(i),end=" ")
Hướng dẫn python recursion practice problems with solutions - vấn đề thực hành đệ quy python với giải pháp

Example 3: Finding the power of a number

The mathematical equation for finding the power of a number is as follows:

p(base, exp) = base * p(b, exp-1)