Skip to content

Instantly share code, notes, and snippets.

@phalt
Created November 13, 2013 10:56
Show Gist options
  • Save phalt/7447202 to your computer and use it in GitHub Desktop.
Save phalt/7447202 to your computer and use it in GitHub Desktop.
Fibonacci sequence with a generator
def fib(n):
a, b = 1, 0
while a < n:
yield a
a, b = b, a+b
# example:
'''
The 'yield' key word allows this function to be iterable.
Doesn't build up a list in memory, returns the 'next' one along.
'''
for n in fib(100):
print n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment