Fibonacci Series In Python Using For Loop'
Last Updated :
16 Feb, 2024
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)
OutputFibonacci 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)
OutputFibonacci 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)
OutputFibonacci 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
NumPy - Fibonacci Series using Binet Formula All of us are familiar with Fibonacci Series. Each number in the sequence is the sum of the two numbers that precede it. So, the sequence goes: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...... In this tutorial, we will implement the same using NumPy with the aid of Binet formula. Binet Formula fn = \left [\lef
3 min read
Fibonacci Search in Python Fibonacci Search is a comparison-based technique that uses Fibonacci numbers to search an element in a sorted array. Let us see Fibonacci Search in Python with help of a problem. Problem statement Given a sorted array arr[] of size n and an element x to be searched in it. Return index of x if it is
3 min read
Understanding for-loop in Python A Pythonic for-loop is very different from for-loops of other programming language. A for-loop in Python is used to loop over an iterator however in other languages, it is used to loop over a condition. In this article, we will take a deeper dive into Pythonic for-loop and witness the reason behind
6 min read
Fibonacci Sequence: Lesson for Kids Fibonacci sequence is a type series where each number is the sum of the two numbers before it. It starts from 0 and 1 usually. The Fibonacci sequence is given by 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on. The numbers in the Fibonacci sequence are also called Fibonacci numbers.In this a
7 min read
Nth multiple of a number in Fibonacci Series in Python Our task is to find the nth multiple of a number in the Fibonacci series which involves identifying Fibonacci numbers that are exactly divisible by a given number m. This means we will check each Fibonacci number one by one and select only those that are multiples of m. Once we find the nth such num
4 min read
Unused variable in for loop in Python Prerequisite: Python For loops The for loop has a loop variable that controls the iteration. Not all the loops utilize the loop variable inside the process carried out in the loop. Example: Python3 # i,j - loop variable # loop-1 print("Using the loop variable inside :") # used loop variabl
3 min read
Use for Loop That Loops Over a Sequence in Python In this article, we are going to discuss how for loop is used to iterate over a sequence in Python. Python programming is very simple as it provides various methods and keywords that help programmers to implement the logic of code in fewer lines. Using for loop we can iterate a sequence of elements
3 min read
Specifying the increment in for-loops in Python Let us see how to control the increment in for-loops in Python. We can do this by using the range() function. range() function range() allows the user to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where
2 min read
Add one Laguerre series to another in Python In this article, we will be looking at the method to add one Laguerre series to another in Python. The product of the exponential of a matrix by a vector is computed using Laguerre polynomials and a Filtered Conjugate Residual (FCR) framework. In Python, we take the Numpy library, and use the polyno
2 min read
How to Create a Dynamic Range for Loop in Python? For Loop is widely used to iterate over sequences in Python. However, there are situations where the range or sequence of the loop needs to be dynamic, and determined at runtime. In this article, we will explore various methods to achieve dynamic range for loops.Using range() with Variablesrange() f
4 min read