|
| 1 | +--- |
| 2 | +id: python-recursion |
| 3 | +title: Recursion in Python |
| 4 | +sidebar_label: Recursion in Python |
| 5 | +sidebar_position: 12 |
| 6 | +tags: |
| 7 | + [ |
| 8 | + Python, |
| 9 | + List in Python, |
| 10 | + Introduction of python, |
| 11 | + Python Syntax, |
| 12 | + Variables, |
| 13 | + Operators, |
| 14 | + Type Casting, |
| 15 | + String, |
| 16 | + Tuple in Python |
| 17 | + ] |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +# Recursion in Python |
| 22 | + |
| 23 | +**Recursion** is a programming technique where a function calls itself directly or indirectly to solve a problem. |
| 24 | +It is often used to break down complex problems into smaller, simpler sub-problems. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## Basic Structure of Recursion |
| 29 | + |
| 30 | +Every recursive function has two main parts: |
| 31 | + |
| 32 | +1. **Base Case** → Stops the recursion (prevents infinite calls). |
| 33 | +2. **Recursive Case** → Function calls itself with modified input. |
| 34 | + |
| 35 | +**Example:** |
| 36 | +```python |
| 37 | +def countdown(n): |
| 38 | + if n == 0: # Base case |
| 39 | + print("Time's up!") |
| 40 | + else: # Recursive case |
| 41 | + print(n) |
| 42 | + countdown(n-1) |
| 43 | + |
| 44 | +countdown(5) |
| 45 | +``` |
| 46 | + |
| 47 | +```python |
| 48 | +Output: |
| 49 | +# 5 |
| 50 | +# 4 |
| 51 | +# 3 |
| 52 | +# 2 |
| 53 | +# 1 |
| 54 | +# Time's up! |
| 55 | +```` |
| 56 | + |
| 57 | +## Factorial using Recursion |
| 58 | + |
| 59 | +Factorial of `n` → `n! = n × (n-1) × (n-2) × ... × 1` |
| 60 | + |
| 61 | +```python |
| 62 | +def factorial(n): |
| 63 | + if n == 0 or n == 1: # Base case |
| 64 | + return 1 |
| 65 | + else: # Recursive case |
| 66 | + return n * factorial(n-1) |
| 67 | + |
| 68 | +print(factorial(5)) # Output: 120 |
| 69 | +``` |
| 70 | + |
| 71 | +## Fibonacci Series using Recursion |
| 72 | + |
| 73 | +Fibonacci sequence → 0, 1, 1, 2, 3, 5, 8, ... |
| 74 | + |
| 75 | +```python |
| 76 | +def fibonacci(n): |
| 77 | + if n <= 1: # Base case |
| 78 | + return n |
| 79 | + else: # Recursive case |
| 80 | + return fibonacci(n-1) + fibonacci(n-2) |
| 81 | + |
| 82 | +for i in range(7): |
| 83 | + print(fibonacci(i), end=" ") |
| 84 | +# Output: 0 1 1 2 3 5 8 |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +## Recursion vs Iteration |
| 89 | + |
| 90 | +* **Iteration (loops):** Uses `for` or `while` loops. |
| 91 | +* **Recursion:** Function calls itself. |
| 92 | + |
| 93 | +**Example: Sum of first n numbers** |
| 94 | + |
| 95 | +Recursive: |
| 96 | + |
| 97 | +```python |
| 98 | +def sum_recursive(n): |
| 99 | + if n == 0: |
| 100 | + return 0 |
| 101 | + return n + sum_recursive(n-1) |
| 102 | + |
| 103 | +print(sum_recursive(5)) # Output: 15 |
| 104 | +``` |
| 105 | + |
| 106 | +Iterative: |
| 107 | + |
| 108 | +```python |
| 109 | +def sum_iterative(n): |
| 110 | + total = 0 |
| 111 | + for i in range(1, n+1): |
| 112 | + total += i |
| 113 | + return total |
| 114 | + |
| 115 | +print(sum_iterative(5)) # Output: 15 |
| 116 | +``` |
| 117 | + |
| 118 | + |
| 119 | +## Advantages of Recursion |
| 120 | + |
| 121 | + Makes code **shorter and cleaner** |
| 122 | + Useful for problems naturally defined recursively (factorial, Fibonacci, tree traversal, divide and conquer algorithms) |
| 123 | + |
| 124 | + |
| 125 | +## Disadvantages of Recursion |
| 126 | + |
| 127 | + **Slower execution** than iteration (due to repeated function calls) |
| 128 | + **Memory usage is high** (function calls are stored in the call stack) |
| 129 | + Risk of **stack overflow error** if base case is missing |
| 130 | + |
| 131 | + |
| 132 | +## Tail Recursion in Python |
| 133 | + |
| 134 | +Tail recursion is when the **recursive call is the last statement** in the function. |
| 135 | +Unlike some languages, Python **does not optimize tail recursion**, so deep recursion may cause errors. |
| 136 | + |
| 137 | +```python |
| 138 | +def tail_sum(n, accumulator=0): |
| 139 | + if n == 0: |
| 140 | + return accumulator |
| 141 | + return tail_sum(n-1, accumulator+n) |
| 142 | + |
| 143 | +print(tail_sum(5)) # Output: 15 |
| 144 | +``` |
| 145 | + |
| 146 | + |
| 147 | +## Practical Example: Binary Search (Recursive) |
| 148 | + |
| 149 | +```python |
| 150 | +def binary_search(arr, target, low, high): |
| 151 | + if low > high: |
| 152 | + return -1 # Not found |
| 153 | + |
| 154 | + mid = (low + high) // 2 |
| 155 | + |
| 156 | + if arr[mid] == target: |
| 157 | + return mid |
| 158 | + elif arr[mid] < target: |
| 159 | + return binary_search(arr, target, mid+1, high) |
| 160 | + else: |
| 161 | + return binary_search(arr, target, low, mid-1) |
| 162 | + |
| 163 | +nums = [1, 3, 5, 7, 9, 11] |
| 164 | +print(binary_search(nums, 7, 0, len(nums)-1)) # Output: 3 |
| 165 | +``` |
| 166 | + |
| 167 | +## Conclusion |
| 168 | + |
| 169 | +* Recursion is a function calling itself to solve smaller sub-problems. |
| 170 | +* Every recursive function must have a **base case** to avoid infinite calls. |
| 171 | +* Useful for problems like factorial, Fibonacci, searching, sorting, and tree/graph traversal. |
| 172 | +* While recursion makes code elegant, it may be slower and consume more memory than iteration. |
0 commit comments