Python Recursion

1) Simple Countdown

Write a recursive function countdown(n) that prints numbers from n down to 1.


2) Sum of First N Numbers

Write a recursive function that returns the sum of 1 through n.

Example: sum_n(4) → 10


3) Factorial

Write a recursive function factorial(n) that returns n!.


4) Print Hello N Times

Write a recursive function print_hello(n) that prints “Hello” n times.


5) Reverse Print

Write a recursive function that prints a string backwards.

Example: input “cat” → output t a c


6) Sum of List Elements

Write a recursive function sum_list(lst) that returns the sum of all elements in a list.


7) Count Digits

Write a recursive function that returns the number of digits in a positive integer.

Example: input 123 → output 3


8) Check Palindrome

Write a recursive function that checks if a word is a palindrome.


9) Find Maximum in List

Write a recursive function to find the largest number in a list.


10) Fibonacci (Simple)

Write a recursive function fib(n) that returns the nth Fibonacci number.

(Note: beginner version without optimization.)


11) Print Numbers from 1 to N

Write a recursion that prints numbers in increasing order.


12) List Length

Write a recursive function to return the length of a list.


13) Count Occurrences

Write a recursive function to count how many times a given value appears in a list.

Example: count_value([1,2,1,3], 1) → 2


14) Power Function

Write a recursive function power(base, exp) to compute base**exp.


15) GCD Using Recursion

Write a recursive function to find the greatest common divisor of two numbers.

(Using Euclid’s algorithm.)


16) Sum of Even Numbers in List

Write a recursive function to return the sum of all even numbers in a list.


17) Print Nested List

Given a nested list of numbers, use recursion to flatten and print all numbers.

Example: [1, [2, 3], 4] → 1 2 3 4


18) Countdown with Steps

Write a recursive function countdown(n, step) that prints numbers from n down to 0 using a fixed step.

Example: countdown(10, 2) → 10 8 6 4 2 0


19) Replace Character Recursively

Write a recursive function to replace all instances of a character in a string.

Example: replace every “a” with “@”.


20) Sum of Digits

Write a recursive function that returns the sum of digits of a number.

Example: sum_digits(123) → 6


🧠 Recursion Concepts Practiced

Pattern
What You Learn

Base Case

Needed to stop recursion

Recursive Step

Function calls itself

Return vs Print

Understanding when to return values

Working with Strings & Lists

Using slicing & indexing

Mathematical Recursion

Math problems solved by recursive logic


📌 Example Structure

Simple Recursion Template


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated