Fibonacci Series in Python Using For Loop

An Italian mathematician “Fibonacci” invented the series named “Fibonacci Series”.The “Fibonacci series” is several sequences that are formed by the addition of two previous numbers.

This Python guide will provide you with the details of the Fibonacci series in Python using For Loop with numerous examples. The following topics are demonstrated in this Python guide:

So, let’s get started!

What is the Fibonacci Series in Python?

In the “Fibonacci series”, the starting two numbers placed at the first and second position are usually constant, which are “0 and 1”. However, very few contributors exclude the “0” and start the series from “1”. After these two numbers, all the upcoming numbers are formed by adding the last two numbers. Let’s understand it using the following example:

Fibonacci Series Sequence = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...

In the above series:

  • The first and second numbers of the series are constant “0 and 1”.
  • The third number “1” in the series is obtained by adding the previous two numbers which are “0+1=1”.
  • The fourth number, “2” in the series, is obtained by adding the previous two numbers, which are “1+1=2”.
  • In this way, any number in the series can easily be found.

The Fibonacci series in Python can be found with different methods such as recursion, for loop, and direct formula. In this article, we only focus on the “for loop” function to create the Fibonacci series in Python. Let’s understand the following examples:

Example 1: Using For Loop With Defined Function to Create a Fibonacci Series

In the example given below, the user-defined function is used along with “for loop” to create the “Fibonacci Series” in Python.

Code:

def fibonacci_series(number):
    sequence_list = []
    if number == 1:
        sequence_list = [0]
    else:
        sequence_list = [0,1]
        for i in range(1, number-1):
            sequence_list.append(sequence_list[i-1] + sequence_list[i])
    return sequence_list

print(fibonacci_series(7))

In the above code:

  • The function named “fibonacci_series” is defined and takes the value of variable “number” as an argument.
  • An empty list is initialized by a variable named “sequence_list”.
  • The “if statement” states that if the input number is equal to “1” then print “0” on the screen because the first number of the Fibonacci series is constant “0”.
  • If the input number is not equal to “1” then the else block statement is executed in the program. In the else block statement, a range function is defined, which starts from 1 to (number-1).
  • The “for loop” is used in the above code to iterate at the specific range number according to input and adds the last two numbers of the list.
  • The sum of the two numbers is then appended to a list named “sequence_list” using the “append()” function.

Output:

The output shows the Fibonacci series of the number range “7”.

Example 2: Using For Loop Without Defined Function to Create a Fibonacci

In the example given below, the “Fibonacci Series” is generated without defining any “function” by simply using the “for loop” statement with the “if-else” condition.

Code:

number = int(input("Enter the Series Range: "))
Number_1 = 0
Number_2 = 1
for number in range(0, number):
           if(number <= 1):
                      value = number
           else:
                      value = Number_1 + Number_2
                      Number_1 = Number_2
                      Number_2 = value
           print(value)

In the following code:

  • The range number is taken from the user and converted into an integer.
  • Two variables saved the value of the first two constants of the Fibonacci series, which are “0 and 1”.
  • The “for loop” used in the above code is to iterate over the input range number, which is starting from 0 and going up to the input number.
  • The “If-else” statement is initialized inside the “for loop” body. If the input number is not equal to “1” and is smaller than “1” then the same value will be returned on screen.
  • If the number is greater than “1” then the previous two numbers will be added and show you a result.
  • These previous numbers will add until the “for loop” iteration reaches the input range number.

Output:

The output shows the sequence of the Fibonacci series having the range “6”.

That’s all from this guide!

Conclusion

In Python, the “Fibonacci series” is created using the “for loop” iteration of any user-defined range. The “Fibonacci series” is created with user defined-function and without defining any function (direct method). The “Fibonacci series” is generated by the addition of the last preceding two numbers and the first and second numbers of the series are constant “0”and “1”. In this article, we generate the “Fibonacci series” using the “for loop” statement along with some inbuilt functions like “append()” (for joining numbers to list) and with conditions like “if-else”.