Python Program to Print Fibonacci Series
You can print the Fibonacci series in Python using a loop with
a, b = 0, 1 and repeatedly updating a, b = b, a + b inside a for loop to print the first n numbers.Examples
Input5
Output0 1 1 2 3
Input1
Output0
Input0
Output
How to Think About It
To print the Fibonacci series, start with the first two numbers 0 and 1. Then, for each next number, add the previous two numbers together. Repeat this process until you have printed the desired count of numbers.
Algorithm
1
Get the number of terms to print from the user.2
Initialize two variables with 0 and 1 for the first two Fibonacci numbers.3
Use a loop to repeat for the number of terms.4
Print the current number.5
Update the two variables to the next two Fibonacci numbers by adding them.6
End the loop after printing all terms.Code
python
n = int(input('Enter number of terms: ')) a, b = 0, 1 for _ in range(n): print(a, end=' ') a, b = b, a + b
Output
Enter number of terms: 5
0 1 1 2 3
Dry Run
Let's trace printing 5 Fibonacci numbers through the code
1
Input
n = 5
2
Initialize
a = 0, b = 1
3
Iteration 1
Print 0, update a=1, b=1
4
Iteration 2
Print 1, update a=1, b=2
5
Iteration 3
Print 1, update a=2, b=3
6
Iteration 4
Print 2, update a=3, b=5
7
Iteration 5
Print 3, update a=5, b=8
| Iteration | a (current) | b (next) | Printed |
|---|---|---|---|
| 1 | 0 | 1 | 0 |
| 2 | 1 | 1 | 1 |
| 3 | 1 | 2 | 1 |
| 4 | 2 | 3 | 2 |
| 5 | 3 | 5 | 3 |
Why This Works
Step 1: Start with first two numbers
We begin with a = 0 and b = 1 because the Fibonacci series starts with 0 and 1.
Step 2: Print current number
In each loop, we print the current number stored in a.
Step 3: Update numbers for next iteration
We update a, b = b, a + b to move forward in the series by setting a to the old b and b to the sum of old a and b.
Alternative Approaches
Using recursion
python
def fib(n): if n <= 1: return n return fib(n-1) + fib(n-2) n = int(input('Enter number of terms: ')) for i in range(n): print(fib(i), end=' ')
Recursion is simple but inefficient for large n due to repeated calculations.
Using a list to store series
python
n = int(input('Enter number of terms: ')) fib_series = [0, 1] for i in range(2, n): fib_series.append(fib_series[i-1] + fib_series[i-2]) print(*fib_series[:n])
Stores all numbers in a list, useful if you need to reuse the series later.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs n times, so the time complexity is O(n).
Space Complexity
Only a few variables are used, so space complexity is O(1).
Which Approach is Fastest?
The iterative approach is fastest and uses constant space, while recursion is slower and uses more memory.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative loop | O(n) | O(1) | Printing series efficiently |
| Recursion | O(2^n) | O(n) | Simple code but inefficient |
| List storage | O(n) | O(n) | When series reuse is needed |
Use
a, b = b, a + b to update Fibonacci numbers in one line cleanly.Beginners often forget to update both numbers simultaneously, causing incorrect sequences.