# Fibonacci series in python program

- Link: https://technotaught.com/fibonacci-series-in-python-program/
- Published: 2020-08-12T16:50:11+05:30
- Author: Ashishkumar Vishwakarma

In this program, you’ll learn to print the fibonacci series in python program

The Fibonacci numbers are the numbers in the following integer sequence.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 ……..

The sequence of numbers, starting with 0 and 1, is created by adding the previous
two numbers. This means to say the nth term is the sum of (n-1)th and (n-2)th term.
For example, the early part of the sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 
55, 89,144, 233, 377, and so on.

You can use [repl ](https://repl.it/)for online compiler for every language.

![fibonacci series in python](https://technotaught.com/wp-content/uploads/2020/08/
fibonacci-series-1024x986.png)

### Method 1

### Fibonacci series in python using a loop **for** **loop** and **while** **loop**

    ```c
    #Python program to generate Fibonacci series until 'n' value
    n = int(input("Enter the value: "))
    a = 0
    b = 1
    sum = 0
    count = 1
    while(count <= n): #for _ in range(count,n+1): 
      #if you want to use for loop then remove while loop
      print(sum, end = " ")
      count += 1   #if you want to use for loop then remove count
      a = b
      b = sum
      sum = a + b
    ```

Output:-

    ```wp-block-verse
    Enter the value: 5
    0 1 1 2 3
    ```

If you want to print the output one by one you need to change line 9, you have to
add “\n” in end, like

    ```wp-block-code
    print(sum, end = " \n")
    ```

### Method 2

### Fibonacci series in python using **List** for n number

    ```c
    def fibonacci(n):
         # return a list of fibonacci numbers
         lis = [0,1]
         for i in range(2,n):
             lis.append(lis[i-2] + lis[i-1])
         return(lis[0:n])

    n = int(input("Enter the value: "))

    x=(list(fibonacci(n)))

    print(*x) #In sequence

    print("------------")

    [print(i) for i in x] # In line by line
    ```

Output:-

    ```wp-block-verse
    Enter the value:5
    0 1 1 2 3
    ------------
    0
    1
    1
    2
    3
    ```

### Method 3

The code is too much simple that is only 2 line code for Fibonacci series but do
not use in competitive coding.

    ```c
    fib = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
    print(*(list(fib[:int(input())])))
    ```

Output:-

    ```wp-block-verse
    5
    0 1 1 2 3
    ```

### Method 4

### Fibonacci series in python using **dynamic programming with generator**

    ```c
    inp = int(input())

    def fibonacci(n):
        a,b = 0,1
        for i in range(n):
            yield a
            a,b = b,a+b

    print(*(list(list(fibonacci(inp)))))
    ```

The output is same as above

### Method 5

### Fibonacci series using  list comprehension

    ```c
    n = int(input())
    fib = [0, 1]
    [fib.append(sum(fib[-2:])) for x in range(n)]
    print(*(fib[:n]))
    ```

The output is same as above

#### Important

Most of the time I use ” **\*()**”

This means :- [1,2,3,4,5,6] -> 1 2 3 4 5 6 i,e remove the bracket from the output.

#### **Recommended Posts:**

[Prime number program in python (3 different way)](https://technotaught.com/prime-number-program-in-python-3-different-way/)

[List & tuple & set & dictionary in Python](https://technotaught.com/list-tuple-set-dictionary-in-python/)

[Python Function-lambda |filter, map, reduce.](https://technotaught.com/python-function-lambda-filter-map-reduce/)

### Share this:

 * [  X ](https://technotaught.com/fibonacci-series-in-python-program/?share=twitter)
 * [  Facebook ](https://technotaught.com/fibonacci-series-in-python-program/?share=facebook)
 * [More](https://technotaught.com/fibonacci-series-in-python-program/#)

 * [  WhatsApp ](https://technotaught.com/fibonacci-series-in-python-program/?share=jetpack-whatsapp)
 * [  LinkedIn ](https://technotaught.com/fibonacci-series-in-python-program/?share=linkedin)
 * [  Email ](https://technotaught.com/fibonacci-series-in-python-program/?subject=%5BShared%20Post%5D%20Fibonacci%20series%20in%20python%20program&body=https%3A%2F%2Ftechnotaught.com%2Ffibonacci-series-in-python-program%2F&share=email)
 * [  Reddit ](https://technotaught.com/fibonacci-series-in-python-program/?share=reddit)
 * [  Tumblr ](https://technotaught.com/fibonacci-series-in-python-program/?share=tumblr)
 * [  Pinterest ](https://technotaught.com/fibonacci-series-in-python-program/?share=pinterest)
 * [  Telegram ](https://technotaught.com/fibonacci-series-in-python-program/?share=telegram)
 * [  Print ](https://technotaught.com/fibonacci-series-in-python-program/#print?share=print)

### _Related_
