Fibonacci, Generators and Python

Python is so beautiful and elegant

See this:

def fib():
    a, b = 0, 1
    while 1:
        yield b
        a, b = b, a+b

if __name__ == '__main__':
    f = fib()
    for i in xrange(1000):
        print f.next()

from: http://www.python.org/dev/peps/pep-0255/

Advertisements

3 Comments on “Fibonacci, Generators and Python”

  1. pythonisms says:

    I agree… love the terse yet readable code. thx for posting.

  2. Tugsioninee says:

    Good internet site / will definitely visit again:D

  3. Dixtosa says:

    difference between xrange and range??


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s