# 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)`

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

Article Tags :