Python – print Fibonacci series

This tutorial of python will deal with printing fibonacci series in python. For this we will take user input to print the number of elements in the series.

So in the first step we take input from user and convert into integer as the input function will return string type.

Next we will define 3 variables two to hold the current and next element and other one to hold the total of first two.

Then the actual logic where we calculate total by adding a + b and change values of a and b to total and a respectively after each calculation.

Combining all the above pieces together will look something like below

#print fibonacci series upto a limit given by user input

#take input from user
num = input("How many elements do you want to print in the fibonacci series: ")

#convert it to integer type as the return type of input() is string
num = int(num)

#initally fibonacci series contains 0 and 1
a = 0 
b = 1

for i in range(1, num+1):
    total = a + b #actual fibonacci series calculation
    a = b
    b = total


Comment how do you feel

Start a Conversation

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