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
- At the start, we use
def recur_fibo(n)where thedefkeyword is used to define a function and therecur_fibo(n)is used to calculate the nth term of the sequence. - After that we declare an
ifstatement followed by a:with conditionn<=1where if the condition is satisfied, thereturnfunction will give the value ofn. If not, it moves to theelsestatement. - We declare the
elsestatement followed by a:with the condition(recur_fibo(n-1) + recur_fibo(n-2)), the value returned by the condition will be displayed using thereturnfunction. - Here we give the user the option to enter the values and the input values are scanned using the
inputfunction and are stored in the variablentermswith the statements/strings("Enter the number of terms: "), we use theintfunction and declare the input value as an integer value. - In the STDIN section of the code editor the input values are entered.
- Following the
inputfunction, we declare anifstatement with conditionnterms<=0followed by a:, if the condition is satisfied theprintfunction will display the statement("Plese enter a positive integer"), if not it moves to theelsestatement. - We declare the
elsestatement followed by a:along with theprintfunction which will display the statement("\nFibonacci sequence:") - We declare a
forloop and the variableialong with therangefunction which will hold the values ofiwith the range(nterms)followed by a:. The loop will iterate till all the values of the range are used in the condition andprintfunction will display the values ofrecur_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.
- A 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.