Fibonacci Sequence in Python

Who knew it could be so easy and gratifying?

Posted by elliotfriend on March 09, 2014

In case anyone ever wanted to know how to run the Fibonacci Sequence in Python, here’s how I did it:

#!/usr/bin/env python
import sys

f_seq = [ 0, 1 ]

while len(f_seq) <= int(sys.argv[1]):
    f_seq.append(f_seq[-1] + f_seq[-2])

print f_seq

Now, all you have to do is run fibonacci.py 20, and it will give you all the numbers through F20 (starting at zero).

Update: I put this up on Github, and have made a couple changes since. Check it out here!