# Fibonacci series in python program

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 for online compiler for every language.

### Fibonacci series in python using a loop forloop and whileloop

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

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

``print(sum, end = " \n")``

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

``````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:-

````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.

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

Output:-

```5
0 1 1 2 3```

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

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

### Fibonacci series using  list comprehension

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

List & tuple & set & dictionary in Python

Python Function-lambda |filter, map, reduce. 