![]() |
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 LogicThe 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 :
Fibonacci Series In Python Using For LoopBelow, are the ways to create Fibonacci Series In Python Using For Loop.
Fibonacci Series In Python Using a ListIn 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
Output
Fibonacci series with 10 elements: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] Fibonacci Series In Python Using VariablesIn this example, below function uses two variables, Python3
Output
Fibonacci series with 10 elements: 0 1 1 2 3 5 8 13 21 34 Fibonacci Series In Python Using Generator FunctionIn 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
Output
Fibonacci series with 10 elements: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] ConclusionIn 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 |
Reffered: https://www.geeksforgeeks.org
Python Programs |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |