Python 3 Script to Print the Fibonacci Series Sequence Using For and While Loop Full Tutorial For Beginners
Welcome folks today in this blog post we will be building a fibonacci
series sequence in python using for and while loop. All the full source code of the application is shown below.
Get Started
In order to get started we need to make an app.py
file and copy paste the following code
First of all we will be using the while
loop to print the series
app.py
# Python Fibonacci series Program using While Loop
# Fibonacci series will start at 0 and travel upto below number
Number = int(input("\nPlease Enter the Range Number: "))
# Initializing First and Second Values of a Series
i = 0
First_Value = 0
Second_Value = 1
# Find & Displaying Fibonacci series
while(i < Number):
if(i <= 1):
Next = i
else:
Next = First_Value + Second_Value
First_Value = Second_Value
Second_Value = Next
print(Next)
i = i + 1
Now we can print the series by the for
loop
app.py
# Python Fibonacci series Program using For Loop
# Fibonacci series will start at 0 and travel upto below number
Number = int(input("\nPlease Enter the Range Number: "))
# Initializing First and Second Values of a Series
First_Value = 0
Second_Value = 1
# Find & Displaying Fibonacci series
for Num in range(0, Number):
if(Num <= 1):
Next = Num
else:
Next = First_Value + Second_Value
First_Value = Second_Value
Second_Value = Next
print(Next)
Now if you execute the python
script by typing the below command
python app.py