Home Web Development Python Python Recursion
Python

Python Recursion

Functions That Call Themselves

Python Recursion
📚 Python 🎓 Beginner Friendly ⏱ 10–15 min read

Introduction

Recursion is a programming technique where a function calls itself to solve a problem. Each recursive call works on a smaller version of the original problem until a stopping condition, called the base case, is reached.

Why Use Recursion?

Recursion is useful for problems that can be divided into smaller, similar problems such as factorials, Fibonacci numbers, searching folders, and traversing trees.

The Base Case

Every recursive function must have a base case. Without one, the function would continue calling itself forever.

def countdown(number):
    if number == 0:
        print("Done!")
        return

    print(number)
    countdown(number - 1)

countdown(5)

Output:

5
4
3
2
1
Done!

Factorial Example

A factorial is the product of all positive integers from 1 up to a given number.

def factorial(n):
    if n == 1:
        return 1

    return n * factorial(n - 1)

print(factorial(5))

Output:

120

Recursive Fibonacci

def fibonacci(n):
    if n <= 1:
        return n

    return fibonacci(n - 1) + fibonacci(n - 2)

print(fibonacci(8))

Advantages

  • Elegant and easy to understand for some problems.
  • Useful for tree structures and nested data.
  • Reduces complex problems into smaller ones.

Disadvantages

  • Can be slower than loops.
  • Uses more memory because each function call is stored.
  • Incorrect recursion can cause a stack overflow.

Best Practices

  • Always define a base case.
  • Ensure each recursive call moves toward the base case.
  • Use recursion only when it makes the solution clearer.
  • Prefer loops for simple repetitive tasks.

Real-World Example

Recursion is commonly used when exploring folder structures.

Folder
 ├── Documents
 │     ├── Report.pdf
 │     └── Notes.txt
 └── Pictures
       ├── Photo1.jpg
       └── Photo2.jpg

A recursive function can visit every folder and file regardless of how deeply they are nested.

Summary

Recursion is a powerful programming technique where functions call themselves to solve smaller versions of a problem. Understanding base cases and recursive calls will help you solve many advanced programming challenges.

Examples

The following examples help reinforce the concepts explained in this lesson.

<!DOCTYPE html>
<html>

<head>

<title>My First Page</title>

</head>

<body>

<h1>Hello World</h1>

</body>

</html>

💡 Pro Tip

Practice every concept immediately after reading it. Learning by doing is the fastest way to master HTML.

⚠ Common Mistake

Do not simply copy code examples. Type them yourself and experiment with small changes.

Best Practices

  • Write clean and readable HTML.
  • Indent your code consistently.
  • Use semantic HTML elements.
  • Validate your HTML regularly.
  • Test your pages in multiple browsers.

Frequently Asked Questions

Why should I learn HTML first?

HTML is the foundation of every website. Once you understand HTML, learning CSS and JavaScript becomes much easier.

Is HTML difficult?

No. HTML is considered one of the easiest web technologies to learn, making it an excellent starting point for beginners.

Ready for the Next Lesson?

Continue learning HTML one lesson at a time and build a solid foundation in modern web development.

Back to HTML Hub

Stay Updated with Neyews

Receive the latest articles about VPN, Technology, Programming, Linux, Artificial Intelligence, Android, Cybersecurity and SEO.