Fibonacci is a series of numbers where one number is the sum of the two preceding numbers. The series starts at 0,1 and can be iterated till infinite. The starting point of the series goes like this: 0, 1, 1, 2, 3, 5, 8,… and so on. In this series, if I pick 2, it is the sum of the last two preceding numbers – 1 and 1.
Mathematical Logic Behind Fibonacci Series
The Fibonacci series works on simple math principles where each number is the sum of the two preceding ones. The series starts with 0 and 1, and from the third number, each number will be the sum of the two preceding ones.
Here’s the mathematical expression of the Fibonacci series:
F(0)=0, F(1)=1, and F(n)=F(n−1)+F(n−2) for n>1
Where F(n) is the nth number in the Fibonacci sequence.
If you were to expand the Fibonacci series, it would look like this:
0,1,1,2,3,5,8,13,21,34,55,89,144,233,...
Ways to Generate Fibonacci Series In Python Using a For Loop
There are three ways you can implement the Fibonacci series through for loop in Python:
- Create Fibonacci Series using Lists in Python
- Create Fibonacci Series using Variables in Python
- Create Fibonacci Series using the Generator Function in Python
Let’s start with the first one.
1. Create Fibonacci Series using Lists in Python
As the name suggests, this method generates a Fibonacci series and stores it inside the list. The following code starts with a list containing the first two Fibonacci numbers [0, 1], then it uses the for loop to add the sum of the last two numbers in the list to the end of the list until the list contains n numbers.
def fibonacci_list(n):
if n <= 0:
return [0]
sequence = [0, 1]
while len(sequence) < n:
next_value = sequence[-1] + sequence[-2]
sequence.append(next_value)
return sequence
# Example usage:
print(fibonacci_list(10))
Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
2. Create Fibonacci Series using Variables in Python
This method uses two variables to keep track of the last two numbers, which makes it more memory-efficient to generate the Fibonacci series.
The following code starts with initialising two variables – a and b to 0 and 1. Then, the given for loop will be iterated n times, which will append the current value of a to the result list and update a and b to the next Fibonacci numbers.
def fibonacci_variables(n):
a, b = 0, 1
result = []
for _ in range(n):
result.append(a)
a, b = b, a + b
return result
# Example usage:
print(fibonacci_variables(10))
Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
3. Create Fibonacci Series using the Generator Function in Python
This method uses the generator function which will generate one fibonacci number at a time. It will be helpful for generating large or infinite sequences.
Like previous method, the followin code starts by initialization of two variables – a and b to 0 and 1. This loop will run n times and the underscore (_) is used as a variable name when the actual value is not needed.
Next, it prints the current value followed by a space. The end=” ” argument is used to ensure that the values are printed on the same line separated by spaces. After printing the current value of a, the function updates a to the current value of b, and b to the sum of the current a and b.
def fibonacci_for_loop(n):
a, b = 0, 1
for _ in range(n):
print(a, end=" ")
a, b = b, a + b
# Example usage:
fibonacci_for_loop(10)
Output:
0 1 1 2 3 5 8 13 21 34
Wrapping Up…
In this tutorial, I went through how you can use a for loop to create a Fibonacci series in Python, including three different approaches. I hope you will find this tutorial useful. For any queries, leave us a comment.