Python - Display Fibonacci sequence numbers

By xngo on June 8, 2019

Fibonacci sequence numbers is list of integers that is the sum of the 2 preceeding Fibonacci numbers. An example of Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ....

#!/usr/bin/python3
# Description: Generate Fibonacci sequence numbers.
 
def fibonacci(n):
    if n == 0: return 0
    elif n == 1: return 1
    else: return fibonacci(n-1) + fibonacci(n-2)
 
def displayFiboSequence(n):
    for i in range(0, n):
        print(fibonacci(i), end=' ')
 
displayFiboSequence(11)
print()

Reference

  • https://en.wikipedia.org/wiki/Fibonacci_number

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.