Python Program to Print Fibonacci Series

In this Python program, you will learn how to generate and print Fibonacci series using loops and conditional statements.

What is a Fibonacci Sequence ?

A Fibonacci sequence is an sequence of integers where the value of the sequence is determined by adding the preceding two values. The first two values in the sequence is 0 and 1. Then comes Next one with (N-1) + (N-2).

Example of a Fibonacci series is
1,1,1,2,3,5,8,13…

How to Print Fibonacci Series in Python?

# Author : Abundantcode.com
# Program Program to print Fibonacci series

No = 10
num1, num2 = 0, 1
count = 0

if No <= 0:
   print("Invalid Number")
elif No == 1:
   print("Fibonacci sequence for limit of ",No,":")
   print(num1)
else:
   print("Fibonacci sequence:")
   while count < No:
       print(num1)
       nth = num1 + num2
       num1 = num2
       num2 = nth
       count += 1

Output

Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34

In the above program, we store the number of Fibonacci sequence to be generated in the variable No and the first two terms are initialized with 0 and 1 in the variables num1 and num2 respectively.

When the number of sequence to be generated is more than 2 , a while loop is used to find the next term in the sequence by adding the previous two sequence variables.