codingstreets
Search
Close this search box.

Python Program: Find Fibonacci Series in Python

Discover the Fibonacci series and its significance in mathematics and programming. Learn how to generate the Fibonacci sequence using Python code, understand the mathematical principles behind it.

Let’s see another Python project.

Table of Contents

Fibonacci Series 

Fibonacci Series is a series in which a number of series is generated by adding two previous numbers. The series starts by default with 0 and 1.

Mathematically,

Fn = Fn-1 + Fn-2

Where Fn denotes the nth term of the Fibonacci series.

The first two terms of this series are:
F0 = 0 (0th term of Fibonacci sequence)
F1 = 1 (1st term of Fibonacci sequence)

Now, by using the above two values we can easily calculate the Fibonacci series:

F2 = F1 + F0 = 0 + 1 = 1
F3 = F2 + F1 = 1 + 1 = 2
F4 = F3 + F2 = 2 + 1 = 3
F5 = F4 + F3 = 3 + 2 = 5
F6 = F5 + F4 = 5 + 3 = 8
F7 = F6 + F5 = 8+ 5 = 13
F8 = F7 + F6 = 13+ 8 = 21

# 0+1=1,  1+1=2,  2+1=3,  3+2=5,  5+3=8,  8+5=13,  13+8=21

E.g., 0, 1, 1, 2, 3, 5, 8,13, 21

Example: Find the Fibonacci series.

				
					List = [0,1]
num1 = 0
num2 = 1

result = num1+num2       #0+1=1

List.append(result)          #[0,1,1]
print(List)                 #[0,1,1]


result = List[2]+List[1]     #1+1=2
List.append(result)          #[0,1,1,2]
print(List)


result = List[3]+List[2]        #1+2=3
List.append(result)             #[0,1,1,2,3]
print(List)
				
			

Explanation:

The Fibonacci series starts by default with List [0,1]  therefore default numbers 0 and 1 defined as num1 and num2 respectively to find the next number.

The variable result is defined as the sum of num1+num2.

The value of the result (num1+num2) is added to the variable List using append() function and displayed using print() function.

Now, List = [0, 1, 1]

To get the next number in the list –

Add first-two previous numbers (from right side) by accessing the elements at the index number.  

Here, we calculate the sum of the second element (List[1], which is 1) and the third element (List[2], which is also 1). The result will be 2.

Using while loop

Example: Find the Fibonacci series using a while loop.

				
					n = 8

first_number = 0
second_number = 1

sum = 0
count = 0

while count <= n:

    print(first_number)

    sum = first_number+second_number
    first_number = second_number
    second_number = sum

    count+=1

#output
[0,1,1,2,3,5,8,13,21]
				
			

Explanation: 

The variable n represents the number of times to generate the Fibonacci series.

The variable first_number and second_number represent 0 and 1 respectively as the default starting numbers of Fibonacci series. 

The code begins with while loop condition count <= n i.e., until the count > n while loop runs. 

Inside the loop, the current value of first_number is displayed. 

To calculate the next number in the Fibonacci series, the addition of first_number and second_number is stored in the variable sum.

Now, the value of first_number and second_number is swapped. The first_number stores the value of previous second_number and the second_number stores the value of sum.

In each loop, the count value is incremented by 1 to keep track of the iterations.

The loop follows these steps until it reaches the count > n and the Fibonacci series is printed.

Note:  if n is 8, the loop will iterate 9 times (count goes from 0 to 8), and the first 9 terms of the Fibonacci series will be printed.

Using def function

Example: Fibonacci series using def function.

				
					Fibo_series = [0,1]
def fibo_sequence(n):
    for x in range(2, n):
        Next_num = Fibo_series[x-1]+Fibo_series[x-2] 
        Fibo_series.append(Next_num)
    return Fibo_series
print(fibo_sequence(8))

#output:
[0, 1, 1, 2, 3, 5, 8, 13]

#rough explanation of adding two previous numbers and generating the next number in series.
#[2-1] + [2-2] = [1] + [0] = 1+0=1     i.e., [0,1,1]
#[3-1] + [3-2] = [2] + [1] = 1+1=2     i.e., [0,1,1,2]
#.......................                                  i.e., [0,1,1,2,3,5,8]
#[7-1] + [7-2] = [6] + [5] = 8+5=13   i.e., [0,1,1,2,3,5,8,13]

				
			

Explanation:

The code begins with the default list of Fibonacci series as Fibo_series[0, 1].

Created a function fibo_sequence that takes a parameter n where the variable n represents the number of times to generate the Fibonacci series.

Inside the function, for loop along with the range() function is used to iterate over the number from 2 to n-1. We started from 2 because we have already initialized the first two numbers (0 and 1) in the Fibo_series.  

Now to calculate the next number in the series, we add two previous numbers from the list.

By decrementing x by 1 in the loop, we access the previous number (Fibo_seq[x-1]) required for the addition. Then, by decrementing i by 2 (i-2), we access the number two steps back (Fibo_seq[x-2]) required for the addition.

Access the elements from the Fibo_series through the index number obtained from Fibo_series[x-1]+Fibo_series[x-2] and store the value in the variable Next_num.    

Now, Next_num (that is the next number) is added to the Fibo_series using the append() method.

for loop iterate through the numbers until it reaches to the n=8 and repeats all steps. Once the loop completes iterating all numbers, the command reaches to return Fibo_series that stores the all next numbers obtained from Next_num.

Finally, call the function fibo_sequence and declare the value of n to it. 

Conclusion

The article provides a comprehensive overview of the Fibonacci series and demonstrates how to generate it using Python programming. It explains the concept of the Fibonacci sequence and its mathematical logic. The article presents a clear and concise Python code example that utilizes a while loop  and def function to generate the series.

Recent Articles