Recursion in Python

Three smiling robots on a blue background, each smaller than the last, passing a yellow box with speech bubble showing process().

Recursion can sound complicated at first, but it is simply a way for a function to call itself. Instead of repeating code or writing long loops, you can let the function handle the repetition by calling itself again with slightly different input. This can be useful for tasks that involve breaking a problem into smaller pieces.

How recursion works

A recursive function is just like any other function, but inside it you write a call to the same function. Each call works on a smaller or simpler version of the original problem until you reach a point where the function stops calling itself. That stopping point is called the base case.

Here, the function keeps calling itself with a smaller number until it reaches 0. The base case is when n == 0. Without a base case, the function would call itself forever.

Why recursion is useful

Recursion can make code shorter and easier to read, especially for problems that naturally break into smaller pieces. Examples include exploring folders inside folders, solving puzzles like the Towers of Hanoi, or working with tree-like data. Although loops can solve the same problems, recursion can sometimes feel more natural.

Example with strings

We can also use recursion to reverse a string such as “LiddleBit”.

This works by peeling off the first letter and adding it to the reversed version of the rest of the string.

Things to try yourself

  1. Write a function that prints the numbers from 1 to 10 using recursion.
  2. Write a function that adds up the numbers from 1 to n using recursion.
  3. Modify the string reversal function so that it checks if the string is a palindrome (the same forwards and backwards).
  4. Experiment with what happens if you remove the base case. What error do you get?

Recursion takes practice, but once you understand the idea of a function calling itself and always having a base case, it becomes a powerful tool in your Python toolkit.

Main Topic

Python Functions

A flat, landscape-style digital illustration shows a man in an orange shirt drinking from a glass of water. A speech bubble next to him contains the function call drink_water() in bold monospaced text.

A beginner-friendly post explaining Python functions, highlighting clarity, reusability, collaboration, and scalability benefits.

Other Tutorials in this Topic

Illustration showing Python function syntax with def, a function name, parentheses, and indented placeholder code.

Defining Python Functions

Beginner-friendly post introducing Python functions, explaining their syntax, naming conventions, and usage, with simple examples and practice…

Two cartoon robots smiling as one passes a yellow box labelled “age” to the other.

Passing Arguments to Python Functions

A beginner-friendly Python tutorial explaining functions, arguments, keyword arguments, and default values, with examples, readability tips, and…

A smiling blue robot opens a red toolbox against a pale green background, holding a silver spanner labelled “lambda” in black.

How to use python Lambda Functions

Beginner-friendly tutorial explaining Python lambda functions, covering syntax, simple examples, practical uses with map, filter, sorted, and…