Chevron Left Icon
Recursion: Python
Lesson 1 of 2
Chevron Right Icon
  1. 1

    The best way to understand recursion is with lots of practice! At first, this method of solving a problem can seem unfamiliar but by the end of this lesson, we’ll have implemented a variety of algo…

  2. 2

    In the previous exercise, we used an iterative function to implement how a call stack accumulates execution contexts during recursive function calls. We’ll now address the conclusion of this funct…

  3. 3

    Now that we’ve built a mental model for how recursion is handled by Python, let’s implement the same function and make it truly recursive. To recap: We want a function that takes an integer as an …

  4. 4

    Excellent job writing your first recursive function. Our next task may seem familiar so there won’t be as much guidance. We’d like a function […] that, given a positive integer as input, retu…

  5. 5

    The previous exercise ended with a stack overflow, which is a reminder that recursion has costs that iteration doesn’t. We saw in the first exercise that **every recursive call spends time on th…

  6. 6

    Let’s use recursion to solve another problem involving lists: […] . We want to write a function that removes nested lists within a list but keeps the values contained. […] Remember ou…

  7. 7

    So far our recursive functions have all included a single recursive call within the function definition. Let’s explore a problem which pushes us to use multiple recursive calls within the fun…

  8. 8

    Data structures can also be recursive. Trees are a recursive data structure because their definition is self-referential. A tree is a data structure which contains a piece of data **and reference…

  1. 1

    This lesson will provide a series of algorithms and an iterative or recursive implementation. Anything we write iteratively, we can also write recursively, and vice versa. Often, the difference i…

  2. 2

    Nice work! We’ll demonstrate another classic recursive function: […] . […] should return the […] th Fibonacci number, where […] is the number given as input. The first two numbers of…

  3. 3

    Fantastic! Now we’ll switch gears and show you an iterative algorithm to sum the digits of a number. This function, […] , produces the sum of all the digits in a positive number as if they wer…

  4. 4

    We’ll use an iterative solution to the following problem: find the minimum value in a list. […] This solution has a linear runtime, or […] , where […] is the number of elements in the l…

  5. 5

    Palindromes are words which read the same forward and backward. Here’s an iterative function that checks whether a given string is a palindrome: […] Take a moment to think about the runtime of…

  6. 6

    All programming languages you’re likely to use will include arithmetic operators like […] , […] , and […] . Let’s pretend Python left out the multiplication, […] , operator. How coul…

  7. 7

    Binary trees , trees which have at most two children per node, are a useful data structure for organizing hierarchical data. It’s helpful to know the depth of a tree, or how many levels make up t…

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo