18
Lesson 12 of 44

Recursion

Recursion is a powerful concept that allows you to solve problems by breaking them down into smaller parts and applying the same logic to each part. In this lesson, you will learn how to use recursion to tackle programming challenges in Python. Learn to employ recursive algorithms and assess their efficiency.

  • 5 theory articles
  • 13 practice exercises
Start lesson

Fundamentals concepts 5

What is recursion

Recursion is a technique in which a function calls itself to solve a problem by dividing it into smaller versions of the same…

Theory
Read more

How to think recursively

Writing a recursive function is easier if you always follow the same three steps. Do not try to follow all the calls in your…

Theory
Read more

Recursion versus loops: cost and mistakes

Everything that is done with recursion can be done with a loop, and vice versa. So why use it? And, above all, when not to?

Theory
Read more

The call stack and the recursion limit

To write recursive functions without surprises you need to know what happens inside Python each time a function calls itself.

Theory
Read more

Mutual and tail recursion and divide and conquer

Besides simple recursion, there are three variants worth recognizing.

Theory
Read more

Practice exercises 13

Calculate power recursively

Create a Python program with a recursive function that calculates the result of raising an integer to the power of another…

Exercise Medium
Solve it

Create a recursive function to multiply

Create a Python program with a recursive function called multiply() that receives two integers entered by the user and returns…

Exercise Medium
Solve it

Calculate Fibonacci series numbers

Create a Python program that uses recursion to calculate a number of the Fibonacci sequence. The sequence starts with the numbers…

Exercise Medium
Solve it

Calculate the factorial of a number

Create a Python program with a recursive function that calculates the factorial of an integer entered by the user.

Exercise Medium
Solve it

Reverse a string recursively

Create a Python program that asks the user for a text and uses a recursive function to reverse it.

Exercise Medium
Solve it

Check palindrome recursively

Create a Python program with a recursive function that checks whether a text is a palindrome. A palindrome is a text that reads…

Exercise Medium
Solve it

Towers of Hanoi

The Towers of Hanoi puzzle has three rods and n discs of different sizes stacked on the first one. They must be moved to the…

Exercise Hard
Solve it

Sum of digits

A sum of digits has a natural recursive definition: the sum of a number is its last digit plus the sum of the rest of the number.

Exercise Easy
Solve it

Flattening nested lists

Lists can contain other lists at any depth. A fixed loop does not work to go through them: recursion follows the shape of the…

Exercise Hard
Solve it

Recursive binary search

Binary search splits a sorted list in half at each step, so it is another natural case for recursion: you search in one of the…

Exercise Medium
Solve it

Fast exponentiation

Computing b^e by multiplying e times is slow. Fast exponentiation takes advantage of the fact that b^e = (b^(e//2))^2 (× b if e…

Exercise Hard
Solve it

Pascal's triangle

In Pascal's triangle each row starts and ends with 1, and each middle number is the sum of the two above it in the previous row…

Exercise Medium
Solve it

Ways to climb stairs

A staircase has n steps and you can climb 1, 2 or 3 steps at a time. In how many different ways can you climb it? It is a…

Exercise Hard
Solve it