Python 3D Academyবাংলা

Recursion in Python

Chapter 17: Recursion

4 short lessons, with 3D animations, quizzes and coding tasks.

The lessons

  1. A function that calls itself

    A recursive function calls itself on a smaller version of the problem, until it reaches a base case small enough to answer straight away.

  2. The call stack

    Every call gets its own frame on top of the call stack, waits there while the calls it makes run, and is removed when it returns its answer.

  3. Recursion on strings and lists

    With strings and lists, the smaller problem is usually the same thing without its first item (s[1:]), and the base case is the empty string or the empty list.

  4. Fibonacci, stairs and remembering answers

    A recursive function that calls itself twice can repeat the same work again and again, and a memo dictionary that remembers each answer makes it fast.