Fibonacci Sequence in Python


Who knew it could be so easy and gratifying?

Posted by elliotfriend on March 9, 2014

Fibonacci Sequence in Python

Who knew it could be so easy and gratifying?

Posted by elliotfriend on March 9, 2014

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

1
2
3
4
5
6
7
8
9
/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 I initially wrote it.

Photo by IPBrian