Fibonacci sequence using list in PYTHON?

55,394

Solution 1

This code puts the first 700 fibonacci numbers in a list. Using meaningful variable names helps improve readability!

fibonacci_numbers = [0, 1]
for i in range(2,700):
    fibonacci_numbers.append(fibonacci_numbers[i-1]+fibonacci_numbers[i-2])

Note: If you're using Python < 3, use xrange instead of range.

Solution 2

You may want this:

In [77]: a = 0
    ...: b = 1
    ...: while b < 700:
    ...:     a, b = b, a+b
    ...:     print a, b
1 1
1 2
2 3
3 5
5 8
8 13
13 21
21 34
34 55
55 89
89 144
144 233
233 377
377 610
610 987

If you wanna store the results in a list, use list.append:

In [81]: a = 0
    ...: b = 1
    ...: fibo=[a, b]
    ...: while b < 70:
    ...:     a, b = b, a+b
    ...:     fibo.append(b)
    ...: print fibo
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

Solution 3

There are two kinds of mistakes you are making; mistakes that are creating errors and mistakes that are affecting readability

Both instances of the phrase [i] should be removed. I believe that you may be thinking it has something to do with iteration or tuples, but that is part of the reason you are getting errors:

myArray1 = [0] 
myArray2 = [1]

while myArray2 < 700:
    myArray1, myArray2 = b, myArray1+myArray2
    print(myArray2)

the other part of the reason you are getting errors is because of the variable b. You don't declare it and it does not belong. This code will iterate correctly if you switch out b with myArray2:

myArray1 = [0] 
myArray2 = [1]

while myArray2 < 700:
    myArray1, myArray2 = myArray2, myArray1+myArray2
    print(myArray2)

then there are some legibility issues. I would change the phrase myArray1 and 2 to a and b respectively. First because it is just too long; second because in python it is called lists, not arrays; third because you are referring to integers, not lists or arrays:

a = [0] 
b = [1]

while b < 700:
    a, b = b, a+b
    print(b)

then, the variables that were myArray1 and 2, but are now a and b; those are integers and they do not need to be expressed as single object lists. so get rid of the brackets around them:

a = 0 
b = 1

while b < 700:
    a, b = b, a+b
    print(b)

Then, the last phrase in this code says print(b). If you have it printing b then the fibonacci sequence you get is missing its first 1. It will read (on separate lines of course) 1,2,3,5,8,13 and so on. It should read 1,1,2,3,5,8,13. You are missing the first 1. So print(b) needs to be changed to print(a):

a = 0 
b = 1

while b < 700:
    a, b = b, a+b
    print(a)

then, if you are expressing more than one variable you can just list all the variables separated by commas equal to all the values separated by commas like this:

a,b,c,d = 1,2,3,4

so for your code that would translate to:

a,b = 0,1

while b < 700:
    a, b = b, a+b
    print(a)

then get rid of that extra space, white space means something in python, though here it doesn't really make a difference:

a,b = 0,1
while b < 700:
    a, b = b, a+b
    print(a)

So all of this so far has just been enough to get you to your original problem: you are getting an iteration (each consecutive value on a seperate line). Below is how you can get a list to any number n:

def fibo(n):
    fibo_list = []
    a,b = 0,1
    while b < n:
        a,b = b,a+b
        fibo_list.append(a)
    print(fibo_list)

hope that helps

Share:
55,394

Related videos on Youtube

Mark Rollin Borja
Author by

Mark Rollin Borja

Updated on June 08, 2021

Comments

  • Mark Rollin Borja
    Mark Rollin Borja almost 3 years

    I have a problem about making a fibonacci sequence to a list, I'm just new to python someone help me please.

    This is my code. I know this is looking wrong or something because it says invalid syntax. I don't know what to do about this really :(

    This code works for a normal code without using a list!

    myArray1 = [0] 
    myArray2 = [1]
    
    while myArray2 < 700:
        myArray1, myArray2 = b[i], myArray1+myArray2[i]
        print(myArray2)
    
    • Martijn Pieters
      Martijn Pieters about 10 years
      In Python that's a list, not an array.
    • Kevin
      Kevin about 10 years
      Where is b defined? And i?
    • Martijn Pieters
      Martijn Pieters about 10 years
      I added a : colon to your code; was that present in your original? You are also using b and i, which you don't assign to anywhere, and replacing your lists with individual values.
    • Max Noel
      Max Noel about 10 years
      Also, you're trying to compare a list to an integer (while myArray2 < 700). That won't raise an error (which is a shame), but I'm fairly certain that's not what you want to do.
    • 2rs2ts
      2rs2ts about 10 years
      You need to show us your whole program, or else we don't know what you've been trying to do.
    • Mark Rollin Borja
      Mark Rollin Borja about 10 years
      sorry sir .. i corrected it already but still i got an error .
    • starsplusplus
      starsplusplus about 10 years
      Probably include the exact error message as well.
    • Bakuriu
      Bakuriu about 10 years
      @MaxNoel Actually it does raise an error on python3: TypeError: unorderable types: list() < int()
  • Mark Rollin Borja
    Mark Rollin Borja about 10 years
    i really thank you for your answer. Okay sir, i'll do my best to learn more about this pl.