Master Python Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Recursion in Python

Recursion is a programming technique in which a function calls itself. A recursive function solves a problem by dividing it into smaller problems and solving them.

CIIT Tip: Think of recursion in a simple way: the function calls itself, but a stopping condition zaroor honi chahiye.

1. What is Recursion?

When a function calls itself from within its own definition, this technique is called recursion.

def hello():
    print("Hello")
    hello()
Important: The above example does not have a stopping condition, so the function will continue calling itself indefinitely. In practical recursion, a base case must be present.

2. Basic Recursive Function

def count_down(number):

    if number == 0:
        return

    print(number)

    count_down(number - 1)


count_down(5)
Output:
5
4
3
2
1

3. What is a Base Case?

A base case is the condition where recursion stops.

def count_down(number):

    if number == 0:
        return

    print(number)

    count_down(number - 1)

Here:

if number == 0:
    return

is the base case. When number == 0, the function does not make any further call.

4. Recursive Case

The recursive case is the part where the function calls itself again.

def count_down(number):

    if number == 0:
        return

    print(number)

    count_down(number - 1)

Here:

count_down(number - 1)

is the recursive call.

5. Recursion Execution Flow

count_down(5)
↓
count_down(4)
↓
count_down(3)
↓
count_down(2)
↓
count_down(1)
↓
count_down(0)
↓
Base Case → Stop

6. Factorial Using Recursion

Factorial is a very common example of recursion.

Example:

5! = 5 × 4 × 3 × 2 × 1
   = 120

Recursive solution:

def factorial(number):

    if number == 0:
        return 1

    return number * factorial(number - 1)


result = factorial(5)

print(result)
Output:
120

7. Factorial Execution

factorial(5)
= 5 * factorial(4)
= 5 * 4 * factorial(3)
= 5 * 4 * 3 * factorial(2)
= 5 * 4 * 3 * 2 * factorial(1)
= 5 * 4 * 3 * 2 * 1
= 120

8. Sum of Numbers Using Recursion

def sum_numbers(number):

    if number == 0:
        return 0

    return number + sum_numbers(number - 1)


result = sum_numbers(5)

print(result)
Output:
15

Calculation:

5 + 4 + 3 + 2 + 1 = 15

9. Power Using Recursion

def power(base, exponent):

    if exponent == 0:
        return 1

    return base * power(base, exponent - 1)


result = power(2, 4)

print(result)
Output:
16

10. Fibonacci Using Recursion

In the Fibonacci sequence, the next number is the sum of the the previous two numbers.

0, 1, 1, 2, 3, 5, 8, 13...
def fibonacci(number):

    if number <= 1:
        return number

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


for i in range(8):
    print(fibonacci(i))
Output:
0
1
1
2
3
5
8
13

11. Recursion with String

Recursion can also be used to reverse a string.

def reverse_string(text):

    if text == "":
        return ""

    return reverse_string(text[1:]) + text[0]


result = reverse_string("CIIT")

print(result)
Output:
TIIC

12. Count Characters Using Recursion

def count_characters(text):

    if text == "":
        return 0

    return 1 + count_characters(text[1:])


result = count_characters("Python")

print(result)
Output:
6

13. Recursion with List

def print_items(items, index=0):

    if index == len(items):
        return

    print(items[index])

    print_items(items, index + 1)


courses = [
    "Python",
    ".NET",
    "Java"
]

print_items(courses)
Output:
Python
.NET
Java

14. CIIT Example 🤓👀

Suppose CIIT has multiple course levels and we need to display them recursively.

def show_courses(courses, index=0):

    if index == len(courses):
        return

    print("Course:", courses[index])

    show_courses(courses, index + 1)


courses = [
    "Python Full Stack",
    ".NET Full Stack",
    "Java Full Stack",
    "Data Science"
]

show_courses(courses)
Output:
Course: Python Full Stack
Course: .NET Full Stack
Course: Java Full Stack
Course: Data Science

15. Recursion vs Loop

Recursion Loop
The function calls itself for or while is used
A base case is required A loop condition is required
Can be useful for complex problems Generally simpler for repeated tasks
There is function call overhead Usually simpler execution

16. Recursion and Call Stack

Each recursive function call creates a new function call frame in memory. This process is managed through the call stack.

factorial(3)

factorial(3)
    ↓
factorial(2)
    ↓
factorial(1)
    ↓
factorial(0)

Base Case

Then results return back:
1
2
6
Important: Agar recursion bahut deep ho jaye ya base case missing ho, Python may raise a RecursionError.

17. Common Mistakes

Mistake 1: Base Case Missing
def test(number):

    print(number)

    test(number - 1)

This function does not have a stopping condition.

Mistake 2: Wrong Base Case
def count_down(number):

    if number == 10:
        return

    count_down(number - 1)

If the starting value is 5, the function can never reach 10.

Remember: In a recursive function, both the base case and the recursive step must be designed correctly.

18. When to Use Recursion?

  • When working with tree structures.
  • In problems such as graph traversal.
  • In divide-and-conquer algorithms.
  • In mathematical problems such as factorial and Fibonacci.
  • When processing nested structures.

19. Interview Points

  • What is recursion?
  • What is a recursive function?
  • What is a base case?
  • What is a recursive case?
  • What is the difference between recursion and a loop?
  • When can a RecursionError occur in recursion?
  • How can factorial be calculated using recursion?
  • How can the Fibonacci sequence be generated using recursion?

CIIT Learning Point

In recursion, a function calls itself. Every recursive function must have a proper base case. Recursion is useful for factorial, Fibonacci, tree structures, and complex problem-solving techniques.

CIIT Practice Tasks

  1. Countdown:
    Create a recursive function that prints a countdown from the given number to 1.
  2. Factorial:
    Create a factorial(number) function that calculates factorial using recursion.
  3. Sum of Numbers:
    Create a sum_numbers(number) function that returns the sum from 1 to number.
  4. Power:
    Create a power(base, exponent) function that calculates the power using recursion.
  5. Reverse String:
    Create a recursive function that reverses the given string.
  6. Count List Items:
    Use recursion to count the total number of items in a list.
  7. Fibonacci:
    Create a recursive function that returns the Fibonacci sequence's number at the given position.

Summary :

Recursion is a technique in which a function calls itself. In a recursive function, the base case stops the recursion, while the recursive case calls the function again with the next smaller problem. Factorial, Fibonacci, sum, power, string and list processing are examples of problems where recursion can be used.