Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Question: Create a generator function to generate Fibonacci numbers.
Answer:

def fibonacci_generator():

  a, b = 0, 1

  while True:

    yield a

    a, b = b, a + b

Example:

fib_gen = fibonacci_generator()
print(next(fib_gen))  # Output: 0
Is it helpful? Yes No

Most helpful rated by users:

©2025 WithoutBook