Fibonacci Series
Print the first N terms of the Fibonacci sequence using a loop.
BeginnerTopic: Loop Programs
Python Fibonacci Series Program
This program helps you to learn the fundamental structure and syntax of Python programming.
# Program to print Fibonacci series up to N terms
n = int(input("Enter number of terms: "))
if n <= 0:
print("Please enter a positive integer.")
else:
a, b = 0, 1
for _ in range(n):
print(a)
a, b = b, a + bOutput
Enter number of terms: 5 0 1 1 2 3
Understanding Fibonacci Series
We maintain two variables (a, b) representing consecutive Fibonacci numbers and update them each iteration.
Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.