Entri Blog
No Result
View All Result
Monday, October 2, 2023
  • State PSC
    • Kerala PSC
      • Kerala PSC Exams
      • Kerala PSC Notification
      • Kerala PSC Exam Calender
      • Kerala PSC Previous Question papers
      • KAS Exam
      • KAS Previous Year Question papers
      • Kerala PSC VEO Notification
      • Kerala Police SI
      • Kerala PSC LDC Notification
      • Kerala PSC LP/UP Assistant
      • Kerala PSC Village Field Assistant Notification
      • Kerala PSC LD Typist Notification
      • Kerala PSC Food Safety Officer
      • Kerala PSC Excise Inspector Notification
      • Kerala PSC BDO Notification
      • Kerala PSC CPO Notification
      • Kerala PSC LGS Notification
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Govt Exams
    • Railway
    • SSC
  • Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • Courses
    • Data Science Course
      • Data Science Malayalam
      • Data Science Training in Kochi
      • Data Science Training in Trivandrum
      • Data Science Course in Calicut
      • Data Science Training in Thrissur
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Stock Market Course in Kannada
      • Options Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
    • Python Programming Course
    • Quantity Surveying Course
    • Performance Marketing Course
    • Practical Accounting Course
      • Tally Course
      • Taxation Course
      • UAE Accounting
      • GST Course
    • Mern Stack Developer Course
      • Full Stack Developer Course in Kochi
      • Full Stack Developer Course in Trivandrum
      • Full Stack Developer Course in Calicut
      • Full Stack Developer Course in Pune
      • Full Stack Developer Course in Bangalore
      • Full Stack Developer Course in Hyderabad
      • Full Stack Developer Course in Chennai
      • Full Stack Developer Course in Coimbatore
      • Full Stack Developer Course in Indore
      • Full Stack Developer Course in Jaipur
    • Other Courses
      • Montessori Teacher Training
      • MEP Course
      • German Language Course
      • OET Coaching
      • Nurses Recruitment Abroad
      • Digital Marketing Training
      • ChatGPT Course
      • Forex Trading Course
      • Yoga Teacher Training Course
  • Others
    • GATE
    • MAT
    • KMAT
    • UPSC
    • TET
      • APTET
      • CTET
      • DSSSB
      • Karnataka TET
      • Kerala TET
      • KVS
      • MPTET
      • SUPER TET
      • TNTET
      • TSTET
      • UPTET
  • Aptitude Questions
Spoken English
Entri Blog
  • State PSC
    • Kerala PSC
      • Kerala PSC Exams
      • Kerala PSC Notification
      • Kerala PSC Exam Calender
      • Kerala PSC Previous Question papers
      • KAS Exam
      • KAS Previous Year Question papers
      • Kerala PSC VEO Notification
      • Kerala Police SI
      • Kerala PSC LDC Notification
      • Kerala PSC LP/UP Assistant
      • Kerala PSC Village Field Assistant Notification
      • Kerala PSC LD Typist Notification
      • Kerala PSC Food Safety Officer
      • Kerala PSC Excise Inspector Notification
      • Kerala PSC BDO Notification
      • Kerala PSC CPO Notification
      • Kerala PSC LGS Notification
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Govt Exams
    • Railway
    • SSC
  • Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • Courses
    • Data Science Course
      • Data Science Malayalam
      • Data Science Training in Kochi
      • Data Science Training in Trivandrum
      • Data Science Course in Calicut
      • Data Science Training in Thrissur
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Stock Market Course in Kannada
      • Options Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
    • Python Programming Course
    • Quantity Surveying Course
    • Performance Marketing Course
    • Practical Accounting Course
      • Tally Course
      • Taxation Course
      • UAE Accounting
      • GST Course
    • Mern Stack Developer Course
      • Full Stack Developer Course in Kochi
      • Full Stack Developer Course in Trivandrum
      • Full Stack Developer Course in Calicut
      • Full Stack Developer Course in Pune
      • Full Stack Developer Course in Bangalore
      • Full Stack Developer Course in Hyderabad
      • Full Stack Developer Course in Chennai
      • Full Stack Developer Course in Coimbatore
      • Full Stack Developer Course in Indore
      • Full Stack Developer Course in Jaipur
    • Other Courses
      • Montessori Teacher Training
      • MEP Course
      • German Language Course
      • OET Coaching
      • Nurses Recruitment Abroad
      • Digital Marketing Training
      • ChatGPT Course
      • Forex Trading Course
      • Yoga Teacher Training Course
  • Others
    • GATE
    • MAT
    • KMAT
    • UPSC
    • TET
      • APTET
      • CTET
      • DSSSB
      • Karnataka TET
      • Kerala TET
      • KVS
      • MPTET
      • SUPER TET
      • TNTET
      • TSTET
      • UPTET
  • Aptitude Questions
No Result
View All Result
Entri Blog
Spoken Eng
  • HTML Tutorial
  • DSA Tutorials
  • Tutorials in Hindi
    • HTML Tutorial for Beginners in Hindi
    • Python Tutorial for Beginners in Hindi
    • GIT and GITHUB Tutorial for Beginners in Hindi
  • Tutorials in Tamil
    • JavaScript Tutorial in Tamil
  • Tutorials in Telugu
    • HTML Tutorial For Beginners in Telugu
    • CSS Tutorial for Beginners in Telugu
    • Bootstrap Tutorial for Beginner in Telugu
  • Tutorials in Kannada
    • HTML Tutorial For Beginners in Kannada
banner top article banner top article
Home Articles

Python Program for Fibonacci Series

by Feeba Mahin
April 27, 2023
in Articles
Python Program for Fibonacci Series
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • What is the Fibonacci Sequence?
  • How to Print the Fibonacci Sequence in Python

 

In this article, we will learn how to print the Fibonacci sequence in python using two different techniques, iteration and recursion.

Before we begin, let’s first understand some basic terminology.

Python and Machine Learning Rectangle

What is the Fibonacci Sequence?

The Fibonacci Sequence is a sequence of numbers in which a given number is the result of adding the 2 numbers that come before it. And adding the previous 2 numbers some number of times forms a series that we call the Fibonacci Series.

The Fibonacci sequence starts with two numbers, that is 0 and 1. Then every following number is made up of adding the previous two numbers together.

For example, take 0 and 1. They’re the first two numbers in the sequence. If you add them together, you get 1. So the sequence starts 0, 1, 1,…

Then, to find the next number, you add the last number you have and the number before it. So 1+1 = 2. So the sequence so far is 0, 1, 1, 2, … Make sense?

We can represent this more mathematically like 0, 1, (1) – [0 + 1]. Similarly, the next Fibonacci number is – 0, 1, 1, (2) – [1 + 1]. And so on. Here’s a diagram showing the first 10 Fibonacci numbers:

Fibonacci-series

This is an example of a Fibonacci series – 0, 1, 1, 2, 3, 5, 8, 13, 21, 34. Within this continuous sequence, every individual number is a Fibonacci number.

Mathematically, the Fibonacci Sequence is represented by this formula:

F(n) = F(n-1) + F(n-2), where n > 1.

We can use this sequence to find any nth Fibonacci number.

“Ready to take your python skills to the next level? Sign up for a free demo today!”

How to Print the Fibonacci Sequence in Python

You can write a computer program for printing the Fibonacci sequence in 2 different ways:

  • Iteratively, and
  • Recursively.

Iteration means repeating the work until the specified condition is met. Recursion, on the other hand, means performing a single task and proceeding to the next for performing the remaining task.

Here’s an iterative algorithm for printing the Fibonacci sequence:

  1. Create 2 variables and initialize them with 0 and 1 (first = 0, second = 1)
  2. Create another variable to keep track of the length of the Fibonacci sequence to be printed (length)
  3. Loop (length is less than series length)
  4. Print first + second
  5. Update first and second variable (first will point to the second, and the second will point to first + second)
  6. Decrement the length variable and repeat from step 3
  7. Once the loop terminates, terminate the program

Iterative Python Code for printing Fibonacci Sequence:

def PrintFibonacci(length):
    #Initial variable for the base case. 
    first = 0
    second = 1

    #Printing the initial Fibonacci number.
    print(first, second, end=" ")

    #decreasing the length by two because the first 2 Fibonacci numbers 
    #already printed.
    length -= 2
    
    #Loop until the length becomes 0.
    while length > 0:

        #Printing the next Fibonacci number.
        print(first + second, end=" ")

        #Updating the first and second variables for finding the next number. 
        temp = second
        second = first + second
        first = temp

        #Decreasing the length that states the Fibonacci numbers to be 
        #printed more.
        length -= 1

if __name__ == "__main__":
    print("Fibonacci Series - ")
    PrintFibonacci(7)
    pass

Output for length 7:

Fibonacci Series - 
1 1 2 3 5 8

Explanation of the Code:

In the above code, first we have defined a function that will print the Fibonacci series. It accepts a parameter for the length, and the function needs to print the Fibonacci series.

Next, we have created 2 variables that contain the initial 2 Fibonacci values, that is 0 and 1.

Then we printed the first 2 values [0, 1] and decremented the length by 2, because 2 values have already been printed.

We will run a loop for the remaining length time, and each time print the next Fibonacci value by adding the previous 2 terms that are stored in the first and second variables (that we created initially to keep track of the previous 2 values).

Update the first and second values that will point to the previous 2 values [first = second, and second = previous first + second].

The loop will run until the length becomes 0, which states that the required length of the Fibonacci sequence is printed.

Then we call the function defined for printing Fibonacci from the main function by passing the argument of the required length to be printed. And there you have it!

There is another approach for printing the Fibonacci sequence using the help of recursion. So, let’s have a look at that approach, too.

“Get hands-on with our python course – sign up for a free demo!”

Recursive Algorithm for printing the Fibonacci Sequence:

  • Accept the value of the previous first and second Fibonacci number as the length to be printed.
  • Check if the length is 0 then terminate the function call.
  • Print the Fibonacci value by adding the previous 2 values received in the parameter of the function (first and second).
  • Recursively call the function for the updated value of the first and second, as well as the decreased value of length.

For this recursive function call, we need to pass the initial value of Fibonacci, that is (0 and 1), in the first and second variables.

To help you understand this algorithm better, let’s see the Python implementation of the algorithms. Then we will look at an example so you can see how this recursive algorithm works.

Python and Machine Learning Rectangle

Recursive Python Code for Printing the Fibonacci Sequence:

def PrintFibonacci(first, second, length):

    #Stop the printing and recursive calling if length reaches 
    #the end.
    if(length == 0):
        return

    #Printng the next Fibonacci number.
    print(first + second, end=" ")

    #Recursively calling the function by updating the value and 
    #decrementing the length.
    PrintFibonacci(second, first+second, length-1)

if __name__ == "__main__":
    #Print initial 2 values.
    print(0,1,end=" ")

    #Calling the Function to print the remaining length 
    #fibonacci series
    PrintFibonacci(0,1,7-2)

Output:

For Length 7 
1 1 2 3 5 8

For Length 10
1 1 2 3 5 8 13 21 34

Explanation of the code:

First, we created a function and perform recursion on it. In that function, we accepted the value of the previous 2 Fibonacci numbers to calculate the current Fibonacci number. And we have a length that keeps track of the base case.

For the base case of recursion, we are checking if the length reaches 0. If it does, then we will terminate the recursive call.

In other cases, we are printing the Fibonacci number by adding the previous 2 Fibonacci numbers.

And then we recursively call the function to print the next Fibonacci value by updating the previous 2 values and decrementing the length.

Now let’s visualize the recursive calls of this function with the help of a recursion tree. The length we want printed is 7.

length-to-be-printed-is-7

Before the recursive call is made, the main function prints the initial 2 values, 0 and 1. And then it passes these values to the recursive function.

main-function-prints-the-initial-2-values.-0-and-1

The Recursive function is printing the value (0 + 1) and recursively calls with the next updated value.

Recursive-function-is-printing-the-value--0---1-

Then the recursive function is printing the value (1 + 1) and recursively calls with the next updated value.

printfibonacci-1-2-3-

Now the recursive function is printing the value (1 + 2) and recursively calls with the next updated value.

printfibonacci-2-3-2-

And then the recursive function is printing the value (2 + 3) and recursively calls with the next updated value.

printfibonacci-3-5-1-

Now the recursive function is printing the value (3 + 5) and recursively calls with the next updated value.

printfibonacci-5-8-0-

Finally, the last call is made. And the length is 0, so it will terminate the recursive call again and the series is printed on the console.

“Experience the power of our web development course with a free demo – enroll now!”

Related Articles

Program for Finding Factorial of a Number in Python Why Python Is Used For Data Science?
Guide for getting a job as a Python Developer Python Advanced Interview Questions and Answers
Best Online Python Course with Certificate What is Type Conversion in Python?
Share62SendShare

Feeba Mahin

Related Posts

Kerala PSC Assistant Sub Inspector Notification 2023 PDF: Apply Link, Vacancy, Eligibility
Articles

Kerala PSC Assistant Sub Inspector (ASI) Notification 2023 PDF: Apply Link, Vacancy, Eligibility

September 27, 2023
Kerala PSC Store Keeper Notification 2023 Out: Apply Link, Eligibility, Vacancy
Articles

Kerala PSC Store Keeper Notification 2023: Apply Link, Eligibility, Vacancy

September 27, 2023
Kerala PSC LD Typist Notification 2023 - Vacancy, Salary, Eligibility
Articles

Kerala PSC LD Typist Notification 2023 PDF: Apply Link, Vacancy, Eligibility

September 27, 2023
Next Post
APPSC Group 4 Recruitment 2022

APPSC Group 4 Recruitment 2022

Latest Posts

  • Yoga Trainer Job Opportunities Abroad
  • Kerala PSC Assistant Sub Inspector (ASI) Notification 2023 PDF: Apply Link, Vacancy, Eligibility
  • Kerala PSC Store Keeper Notification 2023: Apply Link, Eligibility, Vacancy
  • Kerala PSC LD Typist Notification 2023 PDF: Apply Link, Vacancy, Eligibility
  • Kerala Police Constable Recruitment 2023: Notification PDF, Apply Date

Trending Posts

  • states of india and their capitals and languages

    List of 28 States of India and their Capitals and Languages 2023 – PDF Download

    152042 shares
    Share 60814 Tweet 38009
  • List of Government Banks in India 2023: All you need to know

    65081 shares
    Share 26032 Tweet 16270
  • KSDA Recruitment 2023 Apply Online for 9264 FDA SDA Posts – Qualification

    8835 shares
    Share 3534 Tweet 2209
  • TNPSC Group 2 Posts and Salary Details 2022

    39922 shares
    Share 15969 Tweet 9981
  • New Map of India with States and Capitals 2023

    31826 shares
    Share 12730 Tweet 7956

Courses

  • Data Science Course
  • Full Stack Developer Course
  • Data Science Course in Malayalam
  • Full Stack Developer Course in Malayalam
  • Full Stack Developer Course in Hindi
  • Full Stack Developer Course in Tamil
  • Full Stack Developer Course in Telugu
  • Full Stack Developer Course in Kannada
  • Python Programming Course
  • Practical Accounting Course
  • Quantity Surveying Course
  • Stock Market Course
  • Stock Market Course in Malayalam
  • Stock Market Course in Tamil
  • Stock Market Course in Kannada
  • Options Trading Course

Company

  • Become a teacher
  • Login to Entri Web

Spoken English Courses

  • Spoken English Course
  • Spoken English Course in Malayalam
  • Spoken English Course in Hindi
  • Spoken English Course in Tamil
  • Spoken English Course in Telugu
  • Spoken English Course in Kannada
  • Spoken English Course for Housewives
  • Spoken English Course for Working Professionals
  • Spoken English Course for School Students
  • Spoken English Course for College Students
  • Spoken English Course for Job Seekers
  • AI Powered Spoken English Course

Quick Links

  • Aptitude Questions
  • Articles
  • Videos
  • Entri Daily Quiz Practice
  • Current Affairs & GK
  • News Capsule – eBook
  • Preparation Tips
  • Kerala PSC Gold
  • Entri Skilling
  • Kerala PSC

Other Courses

  • OET Coaching Classes
  • Nurse Recruitment Abroad
  • Forex Trading Course
  • Montessori Teachers Training
  • Performance Marketing Course
  • German Language Course
  • MEP Course

Popular Exam

  • IBPS Exam
  • SBI Exam
  • Railway RRB Exam
  • Kerala PSC
  • Tamil Nadu PSC
  • Telangana PSC
  • Andhra Pradesh PSC
  • MPPSC
  • UPPSC
  • Karnataka PSC
  • Staff Selection Commission Exam

© 2021 Entri.app - Privacy Policy | Terms of Service

No Result
View All Result
  • State PSC
    • Kerala PSC
      • Kerala PSC Exams
      • Kerala PSC Notification
      • Kerala PSC Exam Calender
      • Kerala PSC Previous Question papers
      • KAS Exam
      • KAS Previous Year Question papers
      • Kerala PSC VEO Notification
      • Kerala Police SI
      • Kerala PSC LDC Notification
      • Kerala PSC LP/UP Assistant
      • Kerala PSC Village Field Assistant Notification
      • Kerala PSC LD Typist Notification
      • Kerala PSC Food Safety Officer
      • Kerala PSC Excise Inspector Notification
      • Kerala PSC BDO Notification
      • Kerala PSC CPO Notification
      • Kerala PSC LGS Notification
    • TNPSC
    • APPSC
    • TSPSC
    • BPSC
    • Karnataka PSC
    • MPPSC
    • UPPSC
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Govt Exams
    • Railway
    • SSC
  • Skilling
    • Coding
    • Spoken English
    • Stock Marketing
  • Courses
    • Data Science Course
      • Data Science Malayalam
      • Data Science Training in Kochi
      • Data Science Training in Trivandrum
      • Data Science Course in Calicut
      • Data Science Training in Thrissur
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Stock Market Course in Kannada
      • Options Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
    • Python Programming Course
    • Quantity Surveying Course
    • Performance Marketing Course
    • Practical Accounting Course
      • Tally Course
      • Taxation Course
      • UAE Accounting
      • GST Course
    • Mern Stack Developer Course
      • Full Stack Developer Course in Kochi
      • Full Stack Developer Course in Trivandrum
      • Full Stack Developer Course in Calicut
      • Full Stack Developer Course in Pune
      • Full Stack Developer Course in Bangalore
      • Full Stack Developer Course in Hyderabad
      • Full Stack Developer Course in Chennai
      • Full Stack Developer Course in Coimbatore
      • Full Stack Developer Course in Indore
      • Full Stack Developer Course in Jaipur
    • Other Courses
      • Montessori Teacher Training
      • MEP Course
      • German Language Course
      • OET Coaching
      • Nurses Recruitment Abroad
      • Digital Marketing Training
      • ChatGPT Course
      • Forex Trading Course
      • Yoga Teacher Training Course
  • Others
    • GATE
    • MAT
    • KMAT
    • UPSC
    • TET
      • APTET
      • CTET
      • DSSSB
      • Karnataka TET
      • Kerala TET
      • KVS
      • MPTET
      • SUPER TET
      • TNTET
      • TSTET
      • UPTET
  • Aptitude Questions

© 2021 Entri.app - Privacy Policy | Terms of Service