Fibonacci Series in Python

Introduction to Fibonacci Series in Python
The Fibonacci series is a famous mathematical sequence that starts with 0 and 1, and each subsequent number is the sum of the two preceding ones. In Python, you can easily generate the Fibonacci series using various techniques. This page will guide you through different methods to compute and display the Fibonacci series in Python.
What is Fibonacci Series?
The Fibonacci series, also known as the Fibonacci numbers, is a sequence of numbers characterized by the property that each number is the sum of the two preceding ones. The initial terms of this sequence are typically ‘0’ and ‘1’, although in some variations, ‘0’ might be omitted. Consequently, a Fibonacci series can be presented as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. It’s noteworthy that each term in the series is derived by adding the two terms immediately preceding it.
Visualization of Fibonacci Series

Formula for Fibonacci Series
Starting with the first term, F0, set to ‘0’, and the second term, F1, set to ‘1’, subsequent terms are calculated as follows:
- F2 = 0 + 1 = 1
- F3 = 1 + 1 = 2
- F4 = 2 + 1 = 3
- F5 = 2 + 3 = 5
- F6 = 3 + 5 = 8
- F7 = 5 + 8 = 13
- and so forth
To represent any term at position (n+1) in this series, we can use the expression: Fn = Fn-1 + Fn-2. This recursive formula allows us to generate each term in the Fibonacci sequence. The visual representation of a Fibonacci series can be observed in the accompanying image below:

Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Implementation of Fibonacci Series
There are mainly three methods for the implementation of Fibonacci Series in Python :
Method 1: Using Loops
Python allows you to generate the Fibonacci series using loops, such as the for loop or while loop. Here’s an example using a for loop:
def fibonacci_series(n): a, b = 0, 1 print("Fibonacci Series up to", n, "terms:") for _ in range(n): print(a, end=" ") a, b = b, a + b # Example usage fibonacci_series(10)
Fibonacci Series up to 10 terms: 0 1 1 2 3 5 8 13 21 34Explanation:
- The Fibonacci series starts with 0 and 1.
- Each next number is the sum of the previous two numbers.
- Variables a and b are initialized to 0 and 1.
- A for loop runs n times, printing a and updating the values:
- a becomes b
- b becomes a + b (previous value of a + b)
- Efficient for generating Fibonacci series of any positive integer length.
Operation | Time Complexity | Space Complexity |
---|---|---|
Fibonacci Series Using Loop | O(n) | O(1) |
Method 2: Using Recursion
Recursion is another way to generate the Fibonacci series in Python. Here’s a simple recursive function to achieve this:
def fibonacci_recursive(n): if n <= 1: return n else: return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2) # Print first n Fibonacci numbers n = 10 print("Fibonacci Series up to", n, "terms:") for i in range(n): print(fibonacci_recursive(i), end=" ")
Output:
Fibonacci Series up to 10 terms: 0 1 1 2 3 5 8 13 21 34
Explanation:
- The function is defined recursively based on the Fibonacci formula:
F(n) = F(n-1) + F(n-2) - Base cases:
- If n == 0 → return 0
- If n == 1 → return 1
- The recursion builds a call tree and computes each value multiple times.
- Elegant but inefficient for large n due to repeated calculations.
Time and Space Complexity:
Operation | Time Complexity | Space Complexity |
---|---|---|
Fibonacci Series Using Recursion | O(2ⁿ) | O(n) |
Method 3: Using a Generator Function
You can also create a generator function to produce the Fibonacci series dynamically, which is memory-efficient for large sequences:
def fibonacci_generator(): a, b = 0, 1 while True: yield a a, b = b, a + b # Generate and print first 10 Fibonacci numbers gen = fibonacci_generator() print("Fibonacci Series up to 10 terms:") for _ in range(10): print(next(gen), end=" ")
Output:
Fibonacci Series up to 10 terms: 0 1 1 2 3 5 8 13 21 34
Explanation :
- A generator uses the yield keyword to produce values one at a time.
- a and b store the last two Fibonacci numbers.
- The while True loop allows infinite generation; next() gives the next number.
- Memory-efficient and ideal for large sequences due to lazy evaluation.
- No need to store the entire sequence in memory.
Time and Space Complexity:
Operation | Time Complexity | Space Complexity |
---|---|---|
Fibonacci Series Using Generator | O(n) | O(1) |
Properties of Fibonacci Series
The Fibonacci series, a famous mathematical sequence, exhibits several interesting properties:
Additive Property: Each number in the Fibonacci series is the sum of the two preceding numbers. Formally, F(n) = F(n-1) + F(n-2), where F(n) represents the nth Fibonacci number.
Initial Values: The most common starting values for the Fibonacci series are 0 and 1, though other initial values can be used in some variations.
Golden Ratio: As you progress further along the Fibonacci sequence, the ratio between consecutive Fibonacci numbers approaches the golden ratio, approximately 1.61803398875. This makes the Fibonacci sequence closely related to the golden ratio’s mathematical properties.
Divergence: The Fibonacci sequence grows exponentially. As n becomes larger, the values of F(n) grow rapidly. This property has applications in various fields, such as finance, where it’s used to model exponential growth.
Alternating Parity: The parity (whether a number is even or odd) of Fibonacci numbers alternates. For example, F(0) and F(1) are both odd, F(2) is even, F(3) is odd, and so on.
Applications of the Fibonacci Series
The Fibonacci series finds applications in various fields, including:
- Mathematics: It’s a subject of study in number theory and combinatorics.
- Biology: It appears in natural phenomena like the arrangement of leaves, flower petals, and pinecones.
- Finance: The series is used in financial modeling, particularly in the calculation of interest rates.
- Computer Science: Fibonacci numbers are used in algorithms and data structures, such as dynamic programming and graph theory.
To Wrap up with:
The Fibonacci series is not only a fascinating mathematical concept but also a practical tool with diverse applications. Whether you’re exploring its mathematical properties, using it in algorithms, or visualizing its patterns, understanding the Fibonacci series is a valuable skill for Python programmers. Experiment with the provided methods and discover the beauty of this iconic sequence in the world of mathematics and programming.
FAQs
The most efficient way is using dynamic programming or memoization, as it avoids redundant calculations seen in recursion. Iterative or generator-based solutions are also space and time optimized.
In recursion, the function calls itself for the two previous values until it reaches the base case. However, it’s inefficient for large n due to repeated computations and high time complexity.
Yes, Python supports simple iterative or recursive approaches using loops or function calls. The basic logic involves initializing two variables and updating them in each iteration.
Recursive: Time – O(2^n), Space – O(n);
Iterative: Time – O(n), Space – O(1);
DP/Memoization: Time – O(n), Space – O(n);
Generators offer both efficiency and memory optimization.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others