Support Our Site

To ensure we can continue delivering content and maintaining a free platform for all users, we kindly request that you disable your adblocker. Your contribution greatly supports our site's growth and development.

Python Program To Generate Fibonacci Sequence

2 min read

Problem Definition

Make a Python function for generating a Fibonacci sequence. The Fibonacci sequence is a series where the next term is the sum of the previous two terms. The first two terms of the Fibonacci sequence is 0 followed by 1.

Example Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21

Solution

The beauty of Python is that there is always more than one way to tackle the same problem in this article we will go over some of the best methods to generate Fibonacci series in Python.

Method 1: Fibonacci Sequence Using Recursion

n = input('Enter the number of terms')

def fibo(n):
    if n <= 1:
       return n
    else:
       return(fibo(n-1) + fibo(n-2))

for i in range(int(n)):
    print(fibo(i), end=' ')

Output:

Enter the number of terms 6 0 1 1 2 3 5

Method 2: Fibonacci Sequence Using For Loop

n = int(input('Enter the number of terms'))

def Fibonacci(n):
    f0, f1 = 0, 1
    for _ in range(n):
        yield f0
        f0, f1 = f1, f0+f1

fibs = list(Fibonacci(n))print(fibs)

Output:

Enter the number of terms 
6
[0, 1, 1, 2, 3, 5]

Method 3: Fibonacci Sequence Using Lambda and Reduce

from functools import reduce

n = int(input('Enter the Number of terms'))
def fib(n):
    return reduce(lambda x, _: x+[x[-1]+x[-2]], range(n-2), [0, 1])

print(fib(n))

Output:

Enter the Number of terms 6[0, 1, 1, 2, 3, 5]

Method 4: Fibonacci Sequence using lambda and map function

n = int(input('Enter the Number of terms'))

def fibonacci(count):
    fib_list = [0, 1]
    any(map(lambda _: fib_list.append(sum(fib_list[-2:])),
            range(2, count)))
    return fib_list[:count]

print(fibonacci(n))

Output:

Enter the Number of terms 
6
[0, 1, 1, 2, 3, 5]


PROGRAMS

Latest Articles

Latest from djangocentral

How to Use Subquery() in Django With Practical Examples

In the realm of web development, Django stands as a powerful and versatile framework for building robust applications. One of the key aspects of developing efficient and optimized web applications is handling database queries effectively. In this article…
Read more →

4 min read

DRF Serializer: Handling OrderedDict and Converting It to a Dictionary or JSON

In Django Rest Framework (DRF) tests, when you access serializer.data, you might encounter an OrderedDict instead of a regular dictionary. This behavior is intentional and reflects the design of DRF's serialization process.Understanding the Problem The u…
Read more →

3 min read

Django Rest Framework CheetSheet: Mastering API Development

Django Rest Framework (DRF) is a powerful toolkit that makes building robust and scalable web APIs with Django a breeze. Whether you're a seasoned Django developer or a newcomer, having a comprehensive cheat sheet at your disposal can be a game-changer. …
Read more →

5 min read

How to Perform NOT Queries in Django ORM

In Django, performing NOT queries allows you to exclude certain records from the query results based on specific conditions. The NOT operator, represented by the tilde (~) when used in conjunction with the Django ORM's Q object, helps you construct compl…
Read more →

3 min read