Fibonacci Series in Python Using For Loop

(Last Updated On: 27/11/2023)

In this blog, we’ll learn about the Fibonacci series in Python using for loop, What is Fibonacci series in Python for loop, and Why we used the for loop for the Fibonacci Series. Let’s dive into the blog without delay.

What is Fibonacci series in Python?

In Python, the Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, often beginning with 0 and 1. It is described mathematically by the recurrence relation F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.

The Fibonacci series can be created in Python using a variety of approaches such as recursion, iteration, or memoization. One frequent way is to iteratively calculate and store the Fibonacci numbers using a loop. Because of its intriguing mathematical features and self-replicating patterns, this series has applications in many domains, including mathematics, computer science, and nature, and it frequently arises in algorithms, number theory, and dynamic programming issues.

Python Program to Find the Fibonacci Series Using for Loop

The provided Python code generates the Fibonacci series using for loop in Python. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, typically starting with 0 and 1.

# Fibonacci Series in Python Using For Loop
# initialize two variables, with value 0
a, b = 0, 1
series_length = 18

print(a, b, end=' ')
for i in range(series_length):
    c = a + b
    print(c, end=' ')
    a = b
    b = c

Explanation of Fibonacci Series Using For Loop in Python

  • Initialising the Code
    • We initialise the program by defining the two variables ‘a’ and ‘b’ to 0 and 1
    • 0 and 1, because these are the first two values of the Fibonacci series
    • Then we ask the user to input the desired length of the series
    • We define this in code as ‘series_length’
  • Printing the value and Initialising the loop
    • We then print the first two value 0 and 1 by : print(a,b, end=’ ‘)
  • Initialising the ‘for’ loop
    • While initialising the ‘for’ loop we define the number of iteration of the loop by the variable ‘i’, which is also used as a loop counter
    • The maximum value of ‘i’ is equal to the ‘series_length’, but its value is not used in the loop
    • As mentioned in the code, for i in range(series_length), the loop will be executed ‘series_length’ number of times.
  • Iteration and Termination of the loop
    • As we have defined the first two value in the sequence, we are going to calculate the next with an operation adding the two variables
    • Syntax : c = a + b
    • This will give us the value of the next term in the sequence and will be stored as variable ‘c’
    • We are going to print this value in the same line as the first two digit with the print() function
    • Syntax : print(c, end=’ ‘)
    • To proceed with the next iteration, we need to update the values of the ‘a’ and ‘b’ variable.
    • Specifically, ‘a’ is set to current value of ‘b’ and ‘b’ is set to current value of ‘c’
    • After the loop has completed all of terms in the Fibonacci series gets printed

Output:

Check out the Fibonacci series program in Python using for loop code’s output mentioned below:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 

The above code generates a Fibonacci series using ‘for loop’ in Python. It starts by initialising the two variables ‘a’ and ‘b’ to 0 and 1, respectively. Then, the length of the series is set to 18, which is defined by the user.

The program displays the initial two values of the series i.e. 0 and 1, followed by a for loop that runs ‘series_length’ number of times. In each cycle, the program calculates the next value in the series by adding ‘a’ and ‘b’, assigns the value of ‘b’ to ‘a’ and the value of ‘c’ to ‘b’. This process repeats for the specified number of iterations.

The output of the program is a Fibonacci sequence of 18 numbers: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

Why We Used For Loop for Fibonacci Series

The Fibonacci series can also be generated using recursion, generators, and while loop in Python.

Using the recursion method may not be the most efficient method as it involves repeated function calls. Generators offer the advantage of not having to specify the length of the series in advance. They can generate an infinite sequence of numbers and can be stopped at any point. While loops, however, require more lines of code compared to the for loop method.

Overall, the choice of method depends on the specific use case and the tradeoff between elegance and efficiency. In the case of generating a fixed-length Fibonacci series, the for loop method used in the code is a simple and efficient solution. It involves fewer lines of code, is easy to understand, and performs well even for large series lengths. Therefore, this method is generally preferred over other methods for generating the Fibonacci series with a fixed length.

In this Tutorial, we learned about the Fibonacci Series and a Python Program to Find the Fibonacci Series Using the for Loop. In our example we calculated the first 18 terms as the ‘series_length’ variable was defined by the user.

FAQs on the Fibonacci series in Python for loop

What do variables a, b, and c represent in the code?

The variables a and b represent the first and second numbers in the Fibonacci series, 0 and 1, respectively. The variable “c” represents the sum of the previous two numbers in the Fibonacci series.

What is the purpose of the “end” parameter in the print() function?

The “end” parameter specifies what character to print after the value is printed. By default, the “end” parameter is set to “\n” (newline). In this code, the “end” parameter is set to a space character (” “).

What happens if I enter a negative number or zero as the series length?

If you enter a negative number or zero as the series length, the program will result in an error message.

How many terms of the Fibonacci series can I calculate using this program?

There is a limit to how many terms of the Fibonacci series can be calculated. It is based on the available memory and processing power of the computer.

About The Author

Leave a Reply