Python Program to Display the Fibonacci Sequence

In this python tutorial, you will learn how to Display the Fibonacci Sequence using the if and else statements, for loop along with the recursive function of the python programming language.

How to Display the Fibonacci Sequence?

Let’s take a look at the source code , here the values are given as input by the user in the code, recursive function and for loop along with the if and else statements carry out the function.

RUN CODE SNIPPET
# Python program to display the Fibonacci sequence

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

nterms = int(input("Enter the number of terms: "))

# check if the number of terms is valid
if nterms <= 0:
   print("Plese enter a positive integer")
else:
   print("\nFibonacci sequence:")
   for i in range(nterms):
       print(recur_fibo(i))

INPUT:

12

OUTPUT:

Enter the number of terms:
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
55
89
  1. At the start, we use def recur_fibo(n)  where the def keyword is used to define a function and the recur_fibo(n)  is used to calculate the nth term of the sequence.
  2. After that we declare an if statement followed by a : with condition n<=1 where if the condition is satisfied, the return function will give the value of n. If not, it moves to the else statement.
  3. We declare the else statement  followed by a : with the condition (recur_fibo(n-1) + recur_fibo(n-2)) , the value returned by the condition will be displayed using the return function.
  4. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in the variable nterms with the statements/strings ("Enter the number of terms: ") , we use the int function and declare the  input value as an integer value.
  5. In the STDIN section of the code editor the input values are entered.
  6. Following the input function, we declare an if statement with condition nterms<=0  followed by a : , if the condition is satisfied the print function will display the statement ("Plese enter a positive integer") , if not it moves to the else statement.
  7. We declare the else statement  followed by a :   along with the print function which will display the statement ("\nFibonacci sequence:")
  8. We declare a for loop and the variable i along with the range function which will hold the values of  i with the range (nterms) followed by a : . The loop will iterate till all the values of the range are used in the condition and  print function will display the values of  recur_fibo(i) condition.

NOTE:

  •  The def  is the keyword used for defining a function.
  • The if and else statements evaluates whether an expression is true or false. If a condition is true, the “if” statement is executed otherwise, the “else” statement is executed.
  • The colon : at the end of the if and else statement tells Python that the next line of code should only be run if the condition is true.
  • The statement for the input function are enclosed in single quotes and parenthesis.
  • The \n in the code indicates a new line or the end of a statement line or a string.
  • The print statement/string to be displayed in enclosed in double quotes.
  • The input() function allows a user to insert a value into a program, it returns a integer value.
  • For loop runs a block of code until the loop has iterated over every item in a condition, that is it helps to reduce any repetition in the code because it executes the same operation multiple times.
  • The Range() function is used to generate a sequence of numbers, where the range() function is commonly used in for looping functions.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...