How to Create a Fibonacci Sequence in Python


In this example, we will learn us how to create a Fibonacci sequence with Python.

Source Code

The Fibonacci sequence is defined as F (X) +F (X+1) =F (X+2).

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

a = int(input("please input the first num in the sequence: "))

def FB(n):
    if n==1 or n==2:
        return int(a)
    return FB(n-1)+FB(n-2)
print('Last number in Fibonacci Sequence:')
print(FB(10))

Output:

please input the first num in the sequence: 12
Last number in Fibonacci Sequence: 
660
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments