📖 Scenario: You want to find numbers in the famous Fibonacci sequence. This sequence starts with 0 and 1, and each next number is the sum of the two before it. You will write a small program that uses a simple repeating process called recursion to find Fibonacci numbers.
🎯 Goal: Build a Python program that uses a recursive function to find the Fibonacci number at a given position.
📋 What You'll Learn
Create a recursive function named fibonacci that takes one number n.
Use base cases to return 0 when n is 0 and 1 when n is 1.
Use recursion to return the sum of fibonacci(n-1) and fibonacci(n-2) for other values.
Create a variable position to store the Fibonacci position to find.
Print the Fibonacci number at the given position.
💡 Why This Matters
🌍 Real World
Fibonacci numbers appear in nature, computer algorithms, and financial models. Understanding recursion helps solve many problems that break down into smaller similar problems.
💼 Career
Knowing recursion and Fibonacci sequence is a common interview topic for programming jobs. It shows your ability to think recursively and write clean code.
Progress0 / 4 steps
1
Create the Fibonacci function base
Define a function called fibonacci that takes one parameter n. Inside the function, write two base cases: return 0 if n == 0, and return 1 if n == 1.
DSA Python
Hint
Think of the first two Fibonacci numbers as special cases. Use if statements to return them directly.
2
Add the recursive call to Fibonacci function
Inside the fibonacci function, after the base cases, add a return statement that returns the sum of fibonacci(n - 1) and fibonacci(n - 2).
DSA Python
Hint
Use the function itself to find the two previous Fibonacci numbers and add them.
3
Create the position variable
Create a variable called position and set it to 7. This will be the position in the Fibonacci sequence you want to find.
DSA Python
Hint
Just create a variable named position and assign the number 7 to it.
4
Print the Fibonacci number at the position
Use print to display the result of calling fibonacci(position).
DSA Python
Hint
Call the fibonacci function with position and print the result.
Practice
(1/5)
1. What does the Fibonacci sequence represent when calculated using recursion?
easy
A. Each number is the square of the previous number
B. Each number is the product of the two preceding numbers
C. Each number is the difference of the two preceding numbers
D. Each number is the sum of the two preceding numbers
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.
Step 2: Relate to recursion
Recursion calculates this by calling the function for the two previous numbers and adding their results.
Final Answer:
Each number is the sum of the two preceding numbers -> Option D
Quick Check:
Fibonacci = sum of two previous numbers [OK]
Hint: Fibonacci adds two previous numbers, not multiply or subtract [OK]
Common Mistakes:
Thinking Fibonacci multiplies previous numbers
Confusing difference with sum
Assuming Fibonacci squares previous number
2. Which of the following is the correct base case syntax for a recursive Fibonacci function in Python?
easy
A. if n == 0 or n == 1: return n
B. if n < 0: return 0
C. if n == 2: return 1
D. if n == 1: return 0
Solution
Step 1: Identify base cases for Fibonacci
Fibonacci sequence starts with 0 at position 0 and 1 at position 1, so base cases are n == 0 and n == 1.
Step 2: Check syntax correctness
The correct syntax returns n when n is 0 or 1, matching the base cases.
Final Answer:
if n == 0 or n == 1: return n -> Option A
Quick Check:
Base cases return n for 0 or 1 [OK]
Hint: Base cases return n when n is 0 or 1 [OK]
Common Mistakes:
Using wrong base case values
Returning 0 for n == 1
Missing base case for n == 0
3. What is the output of the following code?
def fib(n):
if n == 0 or n == 1:
return n
else:
return fib(n-1) + fib(n-2)
print(fib(4))