×

Python program to find Fibonacci series

Python program to find Fibonacci series

A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence that has integers 0 and 1 at first and second place, and the rest of the sequence term will be the addition of their two previous integers.

Source Code  

The following is the source code to display the Fibonacci series:

 #Program to print Fibonacci series
 #Take user input to print number of terms
 nterms = int(input("How many terms we want:"))
 #Fisrt two term of sequence   
 t1 = 0      
 t2 = 1
 count = 0  
 #Check the validation of number of terms   
 if nterms <= 0:  
     print("Enter positive element")    
 elif nterms == 1:  
   print("Fibonacci series upto",nterms,":")   
print(t1)                                                                                                 else:  
    print("Fibonacci sequence:")        
    while count < nterms:     
print(t1)   
        nth = t1 + t2    
        #Update values      
        t1 = t2            
        t2 = nth      
        count += 1 

Output

Here is the result:

Python program to find Fibonacci series

Explanation: In this program, we have first declared the variable nterms that will store the number of terms to display the Fibonacci series. Next, we need to initialize the first term to 0 and the second term to 1. Whenever the number of terms is more than 2, a while loop is executed to find the next term of the series by adding the previous two values. Next, we will swap the variable, update it, and continue this process to the final terms. Finally, it will display the result on the screen.

Using Recursion

The following is a source code to get the Fibonacci sequence using recursion:

 #Program to print Fibonacci series using recursion                                                                   
 #Create a function to return nth terms of the sequence                                                             
 def recursionFibonacci(n):  
    if n <= 1:      
        return n        
    else:    
        return(recursionFibonacci(n-1) + recursionFibonacci(n-2))                                            
 # take input from the user    
 nterms = int(input("How many terms we want:"))                                                                 
 # check if the number of terms is valid                                                                                        
 if nterms <= 0:   
    print("Enter a positive integer")                                                                                             
 else:  
    print("Fibonacci sequence:")     
    for i in range(nterms):     
        print(recursionFibonacci(i))   

Output

Here is the result:

Python program to find Fibonacci series

Explanation: In this program, we have first declared the variable nterms that will store the number of terms to display the Fibonacci series. Next, calculate the nth term of the sequence using a recursive function recursionFibonacci(). Then, we use the loop for iteration and calculate each term recursively. Finally, it will display the result on the screen.


Related Topics

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

Python float()

Python float() class The float() class in Python returns a floating point number constructed from a number or string x. Syntax class float([x]) Parameter x: This parameter represents a number or a string that can be converted...

1 minute read.

any() Keyword in python

“any” keyword in python This page contains all the information about any () Keyword in python, how it is used with lists, tuples, dictionaries, sets, and what its function is? Python...

3 minutes read.

N2 in Python

N2 is known as Nearest Neighbor Algorithm, because it contains 2 N's (N-Nearest, N-Neighbor). This is a library in the python build using C++ and Python. Before N2 was made,...

3 minutes read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Slicing of a String in Python

In this tutorial, we will learn about slicing of a string in Python. First of all, let us know what String and Slicing is. String String is a group of characters that...

6 minutes read.

Unicode to String in Python

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Image Processing

What is an Image? Images are the pictures that will define the world, and it has their own story, and consists of information about them and these are useful in many...

9 minutes read.

Merge Sort using Python

Merge Sort is a technique that is used for sorting elements in an array using a special method known as divide and conquer. It is the best example of the...

5 minutes read.

Count Number of Keys in Dictionary Python

Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count,...

3 minutes read.

Read numpy array in Python

Numpy is a numerical python that deals with multidimensional arrays mostly used in storing multiple values. Python's core scientific computing package is called NumPy. This Python library provides multidimensional array...

9 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Python Time Module

Python contains many files that can be imported into a python code and used whenever we want. One of that modules is the time module. It is a good practice...

6 minutes read.

Python Dictionary values() method

Python Dictionary values() method The dictionary.values() method in Python returns a view object that displays a list of all the values in the specified dictionary. Syntax dictionary.values() Parameter NA Return This method returns a view object. The...

1 minute read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

Sentence to python vector

Conversion of a Sentence to Vector in Python Before starting the tutorial, let’s just recap about the vector and the respective package that has to be imported in Python. Python Vector: Putting simply,...

3 minutes read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.