We can use generators to produce infinite items. Let’s write a generator function that produces infinite items.
generator_infinite_items.py
def fibonacci():
first = 0
next = 1
while True:
yield first
temp = next
next = first + next
first = temp
# printing first 10 fibonaaci series
i = 0
state = fibonacci()
while i < 10:
print(next(state))
i += 1
Output
0 1 1 2 3 5 8 13 21 34
Previous Next Home
No comments:
Post a Comment