1

Normally people are using in a loop and tried the following code works and wondering how sum leverage yield?

# a generator that yields items instead of returning a list
def firstn(n):
    num = 0
    while num < n:
        yield num
        num += 1

sum_of_first_n = sum(firstn(1000000))

print sum_of_first_n

thanks in advance, Lin

Lin Ma
  • 9,739
  • 32
  • 105
  • 175

0 Answers0