1

Recently an interviewer ask me to create a fibonacci seris of len 10 (n=10) using generator expression. We can do it using generator function -

def fibonacci(n):
    n1, n2, nth, count = 0, 1, 1, 0
    while count < n:
        yield nth
        nth = n1 + n2
        n1 = n2
        n2 = nth
        count += 1


for item in fibonacci(10):
    print(item, end=' ')
print()

OUTPUT: 1 1 2 3 5 8 13 21 34 55

Can we generate same fibonacci series output using generator expression?

kh99
  • 41
  • 1
  • 7
  • Does this answer your question? [Python Fibonacci Generator](https://stackoverflow.com/questions/3953749/python-fibonacci-generator) – hacker315 May 17 '20 at 10:41
  • No @hacker315, The reference question you provided generates fibonacci series using generator function. In my quetion I'm expecting to get same fibonacci series using generator expression if possible. Thanks – kh99 May 17 '20 at 18:00

0 Answers0