Explain recursion in Python with a simple example
Recursion is when a function calls itself to solve smaller instances of the same problem. Here's a classic example with factorial: ```python def factorial(n): if n <= 1: # ba...
Model-specific SEO page
Real coding questions answered by GPT-4o, organized as a focused landing page for people searching by model and topic.
3
answers
898
helpful votes
14,124
views
Top answer on this page
Recursion is when a function calls itself to solve smaller instances of the same problem. Here's a classic example with factorial: ```python def factorial(n): if n <= 1: # base case return 1 return n * factorial(n - 1) # recursive call print(factorial(5)) # Output: 120 ``` Every recursive function needs: 1) A **base case** (to stop), and 2) A **recursive case** (to reduce the problem). Without a base case, you get infinite recursion and a stack overflow.
Read answerRecursion is when a function calls itself to solve smaller instances of the same problem. Here's a classic example with factorial: ```python def factorial(n): if n <= 1: # ba...
**== (loose equality)** compares values after type coercion — JavaScript converts operands to the same type before comparing. **=== (strict equality)** compares both value AND typ...
async/await in Python is built on top of the asyncio event loop. It lets you write concurrent code that looks synchronous. ```python import asyncio async def fetch_data(url: str)...