Fibonacci series in python

Last updated 4 years, 1 month ago | 1584 views 75     5

Tags:- Python

Python | Fibonacci series

This is an example of how to print the Fibonacci series. It takes an input number from the user to print the Fibonacci series.

The Fibonacci series is a sequence of numbers where each number in the sequence is the addition to the previous two consecutive numbers.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ……..

The first two terms are 0 and 1. The rest of the terms are obtained by adding the previous two consecutive numbers. This means the nth term is the sum of (n-1)th and (n-2)th term.

n = int(input('enter no:'))
a,b = 0,1
if n>1:
	print(a,b,end=' ')
	for i in range(2,n):
		a,b = b,a+b
		print(b, end=' ')
 

The output of the above code is:

>>>
=================== RESTART: E:\py\fibonacci_series.py ===================
enter no:10
0 1 1 2 3 5 8 13 21 34 

Same code in the function view

def fab(n):
    if n>2:
        a,b = 0,1
        print(a,b, end=' ')
        for i in range(2,n):
            a,b = b,a+b
            print(b, end=' ')


fab(10)