Fibonacci in Python

Published on 13 October 2020 (Updated: 13 October 2020)

Fibonacci in Python

In this article, we will see how to implement Fibonacci sequence using Python.

How to Implement Solution

Let’s take a look on code for fibonacci.py:

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:])

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, 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 Solution

If we want to run this program, we should probably download a copy of 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.

Further Reading