• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Fibonacci Series In Python

Fibonacci Series In Python

Author: Aditya Raj
Last Updated: January 9, 2022

You might have heard about different types of series of numbers. In this article, we will discuss such a series named the Fibonacci series. The Fibonacci series has a lot of significance in financial markets as it gives a golden ratio that helps in determining the direction towards which any financial market will move. We will also implement a program to find the Fibonacci series in python.

What Is the Fibonacci Series?

The Fibonacci series has been named after the Italian mathematician Fibonacci. In a Fibonacci series, any number at position N is defined as the sum of numbers at position (N-1) and (N-2).

The first and second term of the Fibonacci series has been defined as 0 and 1.

Mathematically, A Fibonacci series F can be defined as follows.

F1=0
F2=1
FN=FN-1+FN-2

Using the above formulae, we can find the number at any position in the Fibonacci Series. For instance,

F3=F2+F1 
  =1+0 
  =1 
F4=F3+F2 
  =1+1 
  =2

We can find the number at any position in the Fibonacci series using the above formula.

How To Determine Fibonacci Series In Python?

To determine the Fibonacci series in python, we can simply use the methodology used above. We can start with the first and second terms and find other terms in the Fibonacci series using a for loop or while loop in python.

For instance, to find the number at the Nth position in the Fibonacci series, we will execute a while loop N-2 times to calculate the terms from the 3rd position to the Nth position. To store the terms, we will use a python list.

fibonacciList = [0, 1]
# finding 10 terms of the series starting from 3rd term
N = 10
term = 3
while term < N + 1:
    value = fibonacciList[term - 2] + fibonacciList[term - 3]
    fibonacciList.append(value)
    term = term + 1
print("10 terms of the fibonacci series are:")
print(fibonacciList)

Output:

10 terms of the fibonacci series are:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Instead of using a while loop, we can also use a for loop to determine the Fibonacci series in Python as follows.

fibonacciList = [0, 1]
# finding 10 terms of the series starting from 3rd term
N = 10
for term in range(3, N + 1):
    value = fibonacciList[term - 2] + fibonacciList[term - 3]
    fibonacciList.append(value)
print("10 terms of the fibonacci series are:")
print(fibonacciList)

Output:

10 terms of the fibonacci series are:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Determine Fibonacci Series Using Recursion In Python

You might be knowing that we can solve a problem using recursion if we can break the problem into smaller sub-problems. As we define a term in the Fibonacci series using its previous terms, we can easily create a recursive solution for determining the term at any position in the Fibonacci series using recursion in Python.

In the recursive solution, we will define a function Fibonacci() that takes a number N as input and returns the term at the Nth position in the Fibonacci series. 

For N=1, the function returns 0 while it returns 1 for N=2. For any other value of N, Fibonacci(N) returns the sum of Fibonacci(N-1) and Fibonacci(N-2). We can implement the solution in python as follows.

def fibonacci(N):
    if N == 1:
        return 0
    if N == 2:
        return 1
    return fibonacci(N - 1) + fibonacci(N - 2)


print("10th term of the fibonacci series is:")
print(fibonacci(10))

Output:

10th term of the fibonacci series is:
34

Conclusion

In this article, we have discussed the Fibonacci series. We have also discussed ways to determine the Fibonacci series in python. To read more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us