# Python Program to Print the Fibonacci Sequence

Fibonacci series is a series formed by whole numbers. It starts with 0 and 1 and includes numbers that are formed by obtaining the sum of the previous two numbers. Thus, the next number after 0 and 1 will be 0+1 = 1. Hence, the third number in the series is 1. Similarly, the fourth number in the series will be 1+1 = 2. The fifth number in the series will be 1+2 = 3, and so on. The Fibonacci sequence looks like 0, 1, 1, 2, 3, 5, 8, 13, and so on.  Thus, in general terms, it can be said that as per the Fibonacci sequence, the nth term will be the sum of (n-1)th term and (n-2)th term of the sequence. Learn Fibonacci python here.

### Source Code

To create the Fibonacci series in Python code, if…else conditions will need to be used. Along with that, a loop will also need to be coded so that the Python code can run until the desired number of terms of the Fibonacci series are printed.

The code below prints the Fibonacci Sequence up to the nth term in Python. Remember that you will have to define the first and second number of the Fibonacci Sequence.

                    
n = int (input (“Enter the number of digits that you want in the Fibonacci sequence: “)

n1, n2 = 0, 1

count = 0

if n <= 0:

print (“The input is invalid. Please enter a positive integer.”)

elif n == 1:

print (“Fibonacci sequence up to n terms will be: “)

print (n1)

else:

print (“Fibonacci sequence up to the given terms will be: “)

while count < n:

print (n1)

nth = n1 + n2

n1 = n2

count = count + 1



In the program, the number of terms of the Fibonacci series provided by the user will be stored in the variable named n. The first and second terms of the sequence are initiated at the beginning of the program using the variables n1 and n2.

When the number of terms of the sequence is more than 2, then the while loop will continue finding the next term by adding the previous two terms. Once the next number of the sequence is found, the values of n1 and n2 are updated by interchanging the values. The loop proceeds until the required number of terms are reached.

Q1. How do you do the Fibonacci Sequence in Python?

In Python, the Fibonacci Sequence can be found in different ways, as suitable for the programmer. One of the most straightforward ways, that is, through the loop, is provided in the source code mentioned above. Another method is recursion. The source code using recursion is provided below.

                    
def Fibonacci (n):

if n < 0:

print (“Invalid input. Series cannot be calculated.”)

elif n == 0:

return 0

elif n == 1 or n == 2:

return 1

else:

return Fibonacci(n-1) + Fibonacci(n-2)

print (Fibonacci (10))



Q2. Is there a Fibonacci function in Python?

There is no special function available in the Python library to calculate the Fibonacci series.

Q3. How do you find the nth Fibonacci number in Python?

To find the nth number in Python, instead of printing the whole series, only the nth number can be printed. The source code will remain the same.

Q4. How do you write a Fibonacci series for a loop in Python?

When coding a loop to print the Fibonacci series, first the conditions of input number of elements of the sequence being negative need to be filtered. Once that is completed, FOR or WHILE loop can be used to execute the code.

Share with friends

## Customize your course in 30 seconds

##### Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Biology
Dr. Nazma Shaik
VTU
Chemistry
Gaurav Tiwari
APJAKTU
Physics
Get Started