Python Programming
June 16, 2025 at 08:08 PM
Today, Let’s move on to the next topic in the Python Coding Challenge: 🔹 *Day 10: Recursion Basics* 🤔 *What is Recursion?* Recursion is when a function calls itself to solve smaller parts of a problem — ideal for tasks that can be broken down into repetitive subproblems. ✅ *Basic Recursive Structure* def function_name(): # base condition if condition: return something else: return function_name() # recursive call Without a base condition, the function will keep calling itself forever — causing a RecursionError. 💡 *Example 1: Factorial Using Recursion* def factorial(n): if n == 0 or n == 1: # base case return 1 else: return n * factorial(n - 1) print(factorial(5)) # Output: 120 *Here’s how it works:* factorial(5) → 5 * factorial(4) → 5 * 4 * factorial(3) → 5 * 4 * 3 * factorial(2) → ... → 5 * 4 * 3 * 2 * 1 = 120 💡 *Example 2: Fibonacci Series Using Recursion* def fibonacci(n): if n == 0: return 0 elif n == 1: return 1 else: return fibonacci(n-1) + fibonacci(n-2) for i in range(7): print(fibonacci(i), end=' ') # Output: 0 1 1 2 3 5 8 🔨 *Mini Project:* *Factorial & Fibonacci Calculator* def factorial(n): return 1 if n <= 1 else n * factorial(n - 1) def fibonacci(n): if n <= 1: return n else: return fibonacci(n - 1) + fibonacci(n - 2) num = int(input("Enter a number: ")) print("Factorial:", factorial(num)) print("Fibonacci:", fibonacci(num)) This covers: - Recursive thinking - Base vs. recursive case - Classic math problems *React with ❤️ once you’re ready for the quiz* Python Coding Challenge: https://whatsapp.com/channel/0029VaiM08SDuMRaGKd9Wv0L/1661
❤️ 👍 🇮🇳 😮 🇵🇸 😢 😂 🙏 477

Comments