Fibonacci Sequence in Python

Today, we are going to learn about Fibonacci Sequence in Python programming language. This program is for mid-level program, CBSE class 11,12 computer science students and Engineering students. It is the start point of mid-level programmers. The syntax here is very well defined. There are more projects like this on this site.

Theory: Fibonacci Sequence in Python

The Fibonacci sequence, in which each number is the sum of the two preceding ones. The sequence commonly starts from 0 and 1.

Step By Step : Fibonacci Sequence in Python

  1. sys file is imported.
  2. Basic knowledge is printed.
  3. Main programming loop is initialised.
  4. Logic is defined.
    a. Entering the Nth Fibonacci number we wish to
    b. Calculating the Nth Fibonacci number
    c. Displaying all the later numbers of the Fibonacci sequence
    d. Displaying the next number in the sequence
    e. Checking if we’ve found the Nth number the user wants

Source code:

import sys
print('''Fibonacci Sequence, by Archit
The Fibonacci sequence begin with 0 and 1, and the coming number is the
sum of the previous two numbers. The sequence is infinite
hence a term needs to be entered for the calculation:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987...
 ''')

while True: # Main programing loop.
    while True: # Keep on asking  until the user enters valid input.
        print('Enter the Nth  Fibonacci number we wish to')
        print('calculate , or QUIT to quit:')
        response = input('> ').upper()

        if response == 'QUIT':
            print('Thanking you for playing!')
            sys.exit()
        if response.isdecimal() and int(response) != 0:
            nth = int(response)
            break # Exiting the loop when the user enters a valid number.
        print('Please enter a number that is greater than 0, or QUIT.')
    print()
    # Handle the special cases if the user enters 1 or 2:
    if nth == 1:
        print('0')
        print()
        print('The first Fibonacci number is 0.')
        continue
    elif nth == 2:
        print('0, 1')
        print()
        print('The second Fibonacci number is 1.')
        continue
     # Displaying warning if the user entered a large number:
    if nth >= 10000:
        print('WARNING: This will taking a while to display on the')
        print('screen. If we want to quit this program before it is')
        print('done, press Ctrl-C.')
        input('Press Enter to begin...')

    # Calculating the Nth Fibonacci number:
    secondToLastNumber = 0
    lastNumber = 1
    fibNumbersCalculated = 2
    print('0, 1, ', end='') # Displaying the first two Fibonacci numbers.

    # Displaying all the later numbers of the Fibonacci sequence:
    while True:
        nextNumber = secondToLastNumber + lastNumber
        fibNumbersCalculated += 1

        # Displaying the next number in the sequence:
        print(nextNumber, end='')

        # Checking if we've found the Nth number the user wants:
        if fibNumbersCalculated == nth:
            print()
            print()
            print('The #', fibNumbersCalculated, ' Fibonacci ',
                  'number is ', nextNumber, sep='')
            break

        # Printing a comma in between the sequence numbers:
        print(', ', end='')

        # Shifting the last two numbers:
        secondToLastNumber = lastNumber
        lastNumber = nextNumber

Output :

Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license()” for more information.

====== RESTART: C:\Users\ARCHIT\Desktop\new python progrm\fibonacci.py ======
Fibonacci Sequence, by Archit
The Fibonacci sequence begin with 0 and 1, and the coming number is the
sum of the previous two numbers. The sequence is infinite
hence a term needs to be entered for the calculation:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987…

Enter the Nth Fibonacci number we wish to
calculate , or QUIT to quit:

15

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377

The #15 Fibonacci number is 377
Enter the Nth Fibonacci number we wish to
calculate , or QUIT to quit:

25

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368

The #25 Fibonacci number is 46368
Enter the Nth Fibonacci number we wish to
calculate , or QUIT to quit:

Conclusion :

The programming is running fine .

Thank you, for learning and copying from Fibonacci Sequence in Python project. This project is a necessary to know for the class 11,12 CBSE students, mid-level programmer and engineering students. For more projects like this click below:
Python Programs Archives – CS Study

Leave a Comment

Your email address will not be published. Required fields are marked *