How to create Fibonacci sequence using PYTHON List

CWC
2 Min Read
Python Program to Alter Iterator

In this article, you will learn how to generate a Fibonacci sequence using Python list programming language and print it in the standard output.

Fibonacci number is one of the most important sequences in mathematics. It is defined as the sum of the two preceding numbers.

The following example shows how to get Fibonacci sequence using the PYTHON List.

How to create Fibonacci sequence using PYTHON List

1

>>> a = 0

>>> b = 1

>>> list1 = []

>>> list2 = []

>>> while True:

... a = b + a

... b = a - b

... if a == b:

... break

... else:

... list1.append(a)

... list2.append(b)

... print(list1)

... print(list2)

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025,…]

[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025,…]

PYTHON List another example


import fibo
def test_fibonacci_number():
assert fibo.fibonacci_number(1) == 1
assert fibo.fibonacci_number(2) == 1
assert fibo.fibonacci_number(3) == 2
assert fibo.fibonacci_number(4) == 2
def test_fibo():
assert fibo.fibonacci_list(1) == [1]
assert fibo.fibonacci_list(2) == [1, 1]
assert fibo.fibonacci_list(3) == [1, 1, 2]

$ py.test test_fibo.py
========================== test session starts
===========================
platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest- 2.5.2
collected 1 items

test_fibo.py F

================================ FAILURES
================================
test_fibo
def test_fibo():
assert mymath.fibo(1) == [1]
assert mymath.fibo(2) == [1, 1]
>	assert mymath.fibo(3) == [1, 1, 2]
E	assert [1, 1, 5] == [1, 1, 2]
E	At index 2 diff: 5 != 2

test_fibo.py:6: AssertionError
======================== 1 failed in 0.01 seconds

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version