Fibonacci Series in Python

Fibonacci Series in Python

Are you finding it hard to code Fibonacci Series in Python? Let me help you with it here. First of all we will introduce our readers a bit about Fibonacci Series and then continue with the Python Code.

What is Fibonacci Series?

The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so forth. Written as a rule, the expression is xn = xn1 + xn2.

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 sequence 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 sequence of Fibonacci series in python upto a specific number.

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

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 break statement to finish the loop there. Other wise, we will append it to our list, swap the numbers and move forward.

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

Share the word to keep supporting me.

Follow me on facebook.

Pin It on Pinterest