Python Program to Print the Fibonacci sequence

In this tutorial, we will discuss how the user can print the Fibonacci sequence of numbers in Python.

Fibonacci sequence:

In the Fibonacci sequence, 1st two number is 1 and 0. The Fibonacci sequence specifies a series of numbers where the next number is found by adding up the two numbers just before. Example of the Fibonacci series is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... and so on.

Python Program to Print the Fibonacci sequence

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … and so on.

In mathematical terms, the sequence "Fn" of the Fibonacci sequence of numbers is defined by the recurrence relation:

Fn= Fn_1+ Fn_2

Where seed values are:

F0=0 and F1=1

Method: 1 - By using a while loop

We will use a while loop for printing the sequence of the Fibonacci sequence.

Step 1: Input the number of values we want to generate the Fibonacci sequence

Step 2: Initialize the count = 0, n_1 = 0 and n_2 = 1.

Step 3: If the n_terms <= 0

Step 4: print "error" as it is not a valid number for series

Step 5: if n_terms = 1, it will print n_1 value.

Step 6: while count < n_terms

Step 7: print (n_1)

Step 8: nth = n_1 + n_2

Step9: we will update the variable, n_1 = n_2, n_2 = nth and so on, up to the required term.

Example 1:

Here we give an example of how to print a Fibonacci series in Python. The example is given below -

Output:

Now we compile the above program in Python, and after compilation, we run it. Then the result is given below -

How many terms the user wants to print?  13
The Fibonacci sequence of the numbers is:
0
1
1
2
3
5
8
13
21
34
55
89
144

Explanation:

In the above code, we have stored the terms in n_terms. We have initialized the first term as "0" and the second term as "1". If the number of terms is more than 2, we will use the while loop for finding the next term in the Fibonacci sequence by adding the previous two terms. We will then update the variable by interchanging them, and it will continue with the process up to the number of terms the user wants to print.

Example 2:

Here we give another example that how to print a Fibonacci series in Python. The example is given below -

Output:

Now we compile the above program in Python, and after compilation, we run it. Then the result is given below -

Enter the number you want to print: 10
0 1 1 2 3 5 8 13 21 34 

In the above code we take user input that how many terms they want to print. Then we initialize a and b with 0 and 1. Then we create a for loop. Then print a and b. After that we initialize a variable c. Then add a and b and store it in variable c. At last, we print the value of c and then the loop is round till the given number by user.

Example 3:

Here we give another example that how to print a Fibonacci series in Python using function. The example is given below -

Output:

Now we compile the above program in Python, and after compilation, we run it. Then the result is given below -

10
0 1 1 2 3 5 8 13 21 34 55

Explanation:

In the above code, we create a function name fibo. Here we add 1st two terms and store them next. Here we use append syntax to store it and print it.

Conclusion:

In this tutorial, we have discussed how the user can print the Fibonacci sequence of numbers to the nth term. The Fibonacci series starts with 0 and 1. Then the series is continued with adding before one. We also give some examples of the Fibonacci series in Python and share the output of it.






Latest Courses