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 thedef
keyword 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
if
statement followed by a:
with conditionn<=1
where if the condition is satisfied, thereturn
function will give the value ofn
. If not, it moves to theelse
statement. - 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 thereturn
function. - 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 variablenterms
with the statements/strings("Enter the number of terms: ")
, we use theint
function and declare the input value as an integer value. - In the STDIN section of the code editor the input values are entered.
- Following the
input
function, we declare anif
statement with conditionnterms<=0
followed by a:
, if the condition is satisfied theprint
function will display the statement("Plese enter a positive integer")
, if not it moves to theelse
statement. - We declare the
else
statement followed by a:
along with theprint
function which will display the statement("\nFibonacci sequence:")
- We declare a
for
loop and the variablei
along with therange
function which will hold the values ofi
with the range(nterms)
followed by a:
. The loop will iterate till all the values of the range are used in the condition andprint
function 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.