Learn how to generate Fibonacci series in Python using a simple program. Step-by-step guide for beginners with explained code, logic, and examples.
The Fibonacci series is a sequence where each number is the sum of the two preceding ones, starting from 0 and 1. It looks like this:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
This pattern is widely used in mathematics, coding interviews, and algorithm development. In this blog, we'll create a Python program to generate the Fibonacci series up to N
terms and explain every step clearly, so even beginners can understand it easily.
A Fibonacci series follows this rule:
F(n) = F(n-1) + F(n-2)
With:
F(0) = 0
F(1) = 1
So:
F(2) = F(1) + F(0) = 1 + 0 = 1
F(3) = F(2) + F(1) = 1 + 1 = 2
and so onβ¦
We will:
Ask the user to enter the number of terms N
.
Print the first two terms manually: 0 and 1.
Use a loop to calculate the next terms using the formula: next = first + second
.
Update the first and second terms.
Continue this until we reach N
terms.
# Program to generate Fibonacci series up to N terms
# Step 1: Ask the user for number of terms
n_terms = int(input("Enter the number of terms: "))
# Step 2: Check if the number is valid
if n_terms <= 0:
print("Please enter a positive number.")
elif n_terms == 1:
print("Fibonacci sequence up to 1 term:")
print(0)
else:
print("Fibonacci sequence:")
first = 0
second = 1
print(first, second, end=" ") # Print first two terms
# Step 3: Use loop to print next terms
for _ in range(2, n_terms):
next_term = first + second
print(next_term, end=" ")
first = second
second = next_term
n_terms = int(input("Enter the number of terms: "))
This line asks the user how many terms of the Fibonacci series they want. The int()
function converts the input string to an integer.
if n_terms <= 0:
print("Please enter a positive number.")
Here we make sure the user gives a positive number. If not, we ask them to try again.
elif n_terms == 1:
print("Fibonacci sequence up to 1 term:")
print(0)
If the user enters 1
, we just print the first Fibonacci number, which is 0
.
print("Fibonacci sequence:")
first = 0
second = 1
print(first, second, end=" ")
We manually print the first two numbers because every Fibonacci sequence starts with 0 and 1.
for _ in range(2, n_terms):
next_term = first + second
print(next_term, end=" ")
first = second
second = next_term
range(2, n_terms)
starts from 2 because we've already printed the first two terms.
We calculate the next number by adding the previous two.
Then we shift the numbers: first
becomes second
, and second
becomes next_term
.
print(..., end=" ")
keeps all numbers on the same line.
Enter the number of terms: 7
Output:
Fibonacci sequence:
0 1 1 2 3 5 8
Helps understand loops, conditions, and variables in Python.
Great for practicing problem-solving.
Commonly asked in coding interviews and college assignments.
Hereβs a reusable function version:
def fibonacci_series(n):
first = 0
second = 1
series = []
if n <= 0:
return []
elif n == 1:
return [0]
series.extend([0, 1])
for _ in range(2, n):
next_term = first + second
series.append(next_term)
first = second
second = next_term
return series
# Example usage
n = int(input("Enter number of terms: "))
print(f"Fibonacci series up to {n} terms: {fibonacci_series(n)}")
You now know how to generate the Fibonacci series in Python using both a simple loop and a function-based approach. This is a great beginner-friendly project to understand the basics of Python programming.
If you're learning Python, the Fibonacci series is a must-do program to build logical thinking and hands-on experience with loops and variables.
Thank you for visiting! Enjoy exploring our diverse collection of blogs, crafted with passion and insight to inspire and inform. Happy reading!