3 ms·
Check it out... def fibonacci_sequence(): a, b, c = 0, 0, 1 while True: a = b; b = c; c = a + b yield b
by empthought 15y ago
Check it out...
def fibonacci_sequence():
a, b, c = 0, 0, 1
while True:
a = b; b = c; c = a + b
yield b
>>> fibonacci_sequence()
<generator object fibonacci_sequence at 0x108055550>
>>> list(itertools.islice(fibonacci_sequence(), 0, 10))
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>>
for n in fibonacci_sequence():
print n
if n > 13:
break
1
1
2
3
5
8
13
21
>>>
- scott_s 15y agoYes, I had forgotten about generators, pjscott and jules provide great examples for the csv case above.