Open In App

Fibonacci Series In Python Using For Loop’

Last Updated : 16 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence goes as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on. This series is not only intriguing due to its inherent mathematical properties but also widely used in various fields such as mathematics, computer science, and nature.

Mathematical Logic

The Fibonacci series follows a simple mathematical logic. The first two numbers in the sequence are 0 and 1. From the third number onward, each number is the sum of the two preceding ones. Mathematically, it can be expressed as:

Syntax :

F(n)=F(n−1)+F(n−2)

where F(n) is the nth number in the Fibonacci sequence.

Fibonacci Series In Python Using For Loop

Below, are the ways to create Fibonacci Series In Python Using For Loop.

Fibonacci Series In Python Using a List

In below, the function uses a list to store the Fibonacci numbers. It starts with the initial values [0, 1] and iterates through the range from 2 to n, calculating each Fibonacci number and appending it to the list.

Python3




def fibonacci_with_list(n):
    fib_series = [0, 1]
    for i in range(2, n):
        fib_series.append(fib_series[-1] + fib_series[-2])
    return fib_series
 
# Example usage:
n = 10
result = fibonacci_with_list(n)
print(f"Fibonacci series with {n} elements:", result)


Output

Fibonacci series with 10 elements: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Fibonacci Series In Python Using Variables

In this example, below function uses two variables, a and b, to store the last two numbers in the Fibonacci sequence. It iterates through the range from 0 to n, updating the variables in each iteration and printing the current Fibonacci number.

Python3




def fibonacci_with_variables(n):
    a, b = 0, 1
    for _ in range(n):
        print(a, end=" ")
        a, b = b, a + b
 
# Example usage:
n = 10
print(f"Fibonacci series with {n} elements:", end=" ")
fibonacci_with_variables(n)


Output

Fibonacci series with 10 elements: 0 1 1 2 3 5 8 13 21 34 

Fibonacci Series In Python Using Generator Function

In this example, below function uses a generator to yield Fibonacci numbers one at a time. It has similar logic to the previous example but is designed to be more memory-efficient, especially for large values of n.

Python3




def fibonacci_generator(n):
    a, b = 0, 1
    for _ in range(n):
        yield a
        a, b = b, a + b
 
# Example usage:
n = 10
result = list(fibonacci_generator(n))
print(f"Fibonacci series with {n} elements:", result)


Output

Fibonacci series with 10 elements: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Conclusion

In this article, we explored the Fibonacci series and its mathematical logic. We then implemented various methods to generate the Fibonacci series in Python using a for loop. Whether using a list, variables, or a generator, the for loop proves to be a versatile tool for efficiently computing the Fibonacci sequence



Similar Reads

Loop Through a List using While Loop in Python
In Python, the while loop is a versatile construct that allows you to repeatedly execute a block of code as long as a specified condition is true. When it comes to looping through a list, the while loop can be a handy alternative to the more commonly used for loop. In this article, we'll explore four simple examples of how to loop through a list us
3 min read
Python | Find fibonacci series upto n using lambda
The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ........ In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation Fn = Fn-1 + Fn-2 with seed values F0 = 0 and F1 = 1. Find the series of fibonacci numbers using lambda function. Code #1 : B
2 min read
Fibonacci Series Program In Python Using Iterative Method
Fibonacci series is a series where each number is the sum of its two previous numbers. In this article, we are going to generate Fibonacci series in Python using Iterative methods. We will be covering both the loops i.e. for loop and while loop. In this article, we will be covering all the concepts related to the topic with clear and concise exampl
4 min read
Python Program for Print Number series without using any loop
Problem - Givens Two number N and K, our task is to subtract a number K from N until number(N) is greater than zero, once the N becomes negative or zero then we start adding K until that number become the original number(N). Note : Not allow to use any loop. Examples : Input : N = 15 K = 5 Output : 15 10 5 0 1 5 10 15 Input : N = 20 K = 6 Output :
3 min read
Python Program for nth multiple of a number in Fibonacci Series
Given two integers n and k. Find position the nth multiple of K in the Fibonacci series. Examples: Input: k = 2, n = 3Output: 9, 3rd multiple of 2 in Fibonacci Series is 34 that appears at position 9. Input: k = 4, n = 5 Output: 30, 5th multiple of 4 in Fibonacci Series is 832040 which appears at position 30. An Efficient Solution is based on the b
5 min read
Python | Plotting Fibonacci spiral fractal using Turtle
What is Fractal Geometry? Fractal geometry is a special form of graphical representation of mathematical functions or set of numbers generated by a mathematical function. It is a recursive, detailed and infinitely self-similar set of mathematics. These geometrical representations exhibit "Unfolding Symmetry". Unfolding symmetry is the property of g
3 min read
Python Program to Display Fibonacci Sequence Using Recursion
We are given a task to write the Fibonacci sequence using recursion. we will take the range as input of integer and then print the Fibonacci Sequence. In this article, we will see the method of Python Program to Display Fibonacci Sequence Using Recursion. Example: Input: n = 9Output: 0 1 1 2 3 5 8 13 21Explanation: Here, we have n = 9, and we print
2 min read
Python - Create list of tuples using for loop
In this article, we will discuss how to create a List of Tuples using for loop in Python. Let's suppose we have a list and we want a create a list of tuples from that list where every element of the tuple will contain the list element and its corresponding index. Method 1: Using For loop with append() method Here we will use the for loop along with
2 min read
How to Initialize a Dictionary in Python Using For Loop
When you want to create a dictionary with the initial key-value pairs or when you should transform an existing iterable, such as the list into it. You use string for loop initialization. In this article, we will see the initialization procedure of a dictionary using a for loop. Initialize Python Dictionary Using For LoopA Dictionary is a collection
3 min read
How to Store Values in Dictionary in Python Using For Loop
In this article, we will explore the process of storing values in a dictionary in Python using a for loop. As we know, combining dictionaries with for loops is a potent technique in Python, allowing iteration over keys, values, or both. This discussion delves into the fundamentals of Python dictionaries and provides a demonstration of how to effect
3 min read
Article Tags :