Python Program to Print the Fibonacci Sequence

The Fibonacci sequence is a series of numbers in which each number is generated by adding the two previous numbers.

The sequence is generally started from 0 and 1, although some authors start the sequence from 1.  Starting from 0 and 1, the first few values in the sequence are

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073, 4807526976, 7778742049, 12586269025, … and so on.


Example 1: Printing Fibonacci using python For loop

# Prompt user for input
n = int(input("Enter the number of terms: "))

# Define variables for first and second terms
a = 0
b = 1

# Print first two terms
print(a)
print(b)

# Use a for loop to print the remaining terms
for i in range(2, n):
    # Compute the next term
    c = a + b
    # Print the next term
    print(c)
    # Update the variables for the next iteration
    a = b
    b = c

In this example first we initialize two variables to the first two numbers in the sequence and then use a for loop to add the two previous numbers together to generate the next number in the sequence.

The loop continues until the desired number of terms in the sequence has been generated, and each number is printed as it is generated.

Example 2: Using Recursion technique to Print Fibonacci Sequence

# Define a function to compute the nth Fibonacci term
def fibonacci(n):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)

# Prompt user for input
num_terms = int(input("Enter the number of terms: "))

# Use a for loop to print the Fibonacci sequence
for i in range(num_terms):
    print(fibonacci(i))

In this example we use a recursion to generate the Fibonacci sequence.

Recursion is a technique in which a function calls itself to solve a problem. In this example the fibonacci() function takes an argument that specifies the number of terms to generate in the sequence.

Inside the function, we use an if statement to handle the initial case (when the number of terms is 0 or 1), and otherwise call itself twice and add the results together to generate the next number in the sequence.

The function then returns the sequence as a list, which is printed to the console. This approach is elegant and can be easier to read than the while loop approach, but it can be slower and may use more memory

Example 3: Using a while loop to print the Fibonacci series

# Prompt user for input
n = int(input("Enter the number of terms: "))

# Define variables for first and second terms
a = 0
b = 1

# Print first two terms
print(a)
print(b)

# Use a while loop to print the remaining terms
i = 2
while i < n:
    # Compute the next term
    c = a + b
    # Print the next term
    print(c)
    # Update the variables for the next iteration
    a = b
    b = c
    i += 1

Leave a Comment