Fibonacci Series in Python

Fibonacci Series in Python

The Fibonacci series in python or general is a sequence of integer numbers such as:

0, 1, 1, 2, 3, 5, 6,13, 21, 34, 55, 89, 144, 233, ….

As you can see that it has such a sequence in which the next number is calculated by adding up the two numbers before it.

The mathematical formulae of the Fibonacci series are defined by the following equation.

Fn = Fn-1 + Fn-2

with seed or starting value of n as 0 and 1. So this makes it

F0 = 0 and F1 = 1

Code Fibonacci Series in Python

As you can understand from the explanation given above that we need to add two numbers and get the number of sequences then we have to swap and then move forward again.

There are multiple ways of doing this in Python. We will keep it short and simple for our readers. We will code to get a sequence of Fibonacci series in python up to a specific number.

All code of Fibonacci Series in Python here on

Method 1 (Using Loop)

nums = []
a = 0
b = 1
findTill = 10000
while True:
   c = a + b
   if c >= findTill:
   a = b
   b = c

Output: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765]

With the code above, we are basically starting from 0 and 1 as our starting members and moving up to 10000.

We are first of all adding both numbers and then checking them if they are exceeding our set limit. If they are exceeding we have a break statement to finish the loop there. Otherwise, we will append it to our list, swap the numbers and move forward.

Method 2 (Using recursion)

def Fibonacci(n):
    if n < 0:
        print("Wrong number")
    elif n == 1:
        return 0
    elif n == 2:
        return 1
        return Fibonacci(n-1) + Fibonacci(n-2)


Output: 21

In this method, what we made is used to find out the specific Fibonacci number in the sequence on a given index n.

I have solved Project Euler Problem 2 Solution which includes Fibonacci Series up to 4 million.

Share the word to keep supporting me.


I am a Software Engineer with ample experience in making games, websites, mobile apps and augmented reality solutions.

Pin It on Pinterest