Fibonacci in Python

Published on 06 October 2018 (Updated: 15 May 2023)

Welcome to the Fibonacci in Python page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

import sys


def fibonacci(n):
    fib = fibs()
    for i in range(1, n + 1):
        print(f'{i}: {next(fib)}')


def fibs():
    first = 1
    second = 1
    yield first
    yield second
    while True:
        new = first + second
        yield new
        first = second
        second = new


def main(args):
    try:
        fibonacci(int(args[0]))
    except (IndexError, ValueError):
        print("Usage: please input the count of fibonacci numbers to output")
        sys.exit(1)


if __name__ == "__main__":
    main(sys.argv[1:])

Fibonacci in Python was written by:

This article was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

Now, we will consider this code block by block on the order of execution.

if __name__ == "__main__":
    main(sys.argv[1:])

This code checks if the main module is run. If it is, it then passes control to main function passing argument string provided by the user.

def main(args):
    try:
        fibonacci(int(args[0]))
    except (IndexError, ValueError):
        print("Usage: please input the count of fibonacci numbers to output")
        sys.exit(1)

This main function was invoked earlier with argument string. Next line invokes fibonacci() function. If the function raises IndexError (Raised when a sequence subscript is out of range) or ValueError (Raised for an non-integer value), it prints correct usage pattern. And program exits with exit status 1 which specifies abnormal termination.

def fibonacci(n):
    fib = fibs()
    for i in range(1, n + 1):
        print(f'{i}: {next(fib)}')

def fibs():
    first = 1
    second = 1
    yield first
    yield second
    while True:
        new = first + second
        yield new
        first = second
        second = new

In fibonacci() function, function fibs() is called. In fibs(), yield function returns generator which iterates to store and get values. Value of first and second are initially stored in generator as 1 and 2. In the while loop, values of fibonacci sequence is added using rule third_num = first_num + second_num. Control goes back to fibonacci() which prints values returned by next() which returns next item in iterator. This sequence is repeated till the user specified input times.

How to Run the Solution

Fibonacci in Python. After that, we should make sure we have the latest Python interpreter. From there, we can simply run the following command in the terminal:

python fibonacci.py

Alternatively, we can copy the solution into an online Python interpreter and hit run.