UHG
Search
Close this search box.

Fibonacci Series in Python using For Loop 

You can use the for loop to create the Fibonacci series in Python efficiently. It reduces complexity and improves the readability of the code as well.

Share

Fibonacci Series In Python Using For Loop
Table of Content

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.

📣 Want to advertise in AIM? Book here

Related Posts
19th - 23rd Aug 2024
Generative AI Crash Course for Non-Techies
Upcoming Large format Conference
Sep 25-27, 2024 | 📍 Bangalore, India
Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.
Flagship Events
Rising 2024 | DE&I in Tech Summit
April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore
Data Engineering Summit 2024
May 30 and 31, 2024 | 📍 Bangalore, India
MachineCon USA 2024
26 July 2024 | 583 Park Avenue, New York
MachineCon GCC Summit 2024
June 28 2024 | 📍Bangalore, India
Cypher USA 2024
Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA
Cypher India 2024
September 25-27, 2024 | 📍Bangalore, India
discord-icon
AI Forum for India
Our Discord Community for AI Ecosystem, In collaboration with NVIDIA.