Skip to content

Instantly share code, notes, and snippets.

Last active Jul 23, 2020
What would you like to do?
g=(n for n in range(1,11))
#Returns an generator object which is an iterator [contains element from 1 to 10]
print (g) #Output: <generator object <genexpr> at 0x0116C728>
#We can iterate through the iterator object using for loop or using next() function.
print (next(g))#Output:1
print (next(g))#Output:2
print (next(g))#Output:3
#for loop will iterate through the remaining elements (starting from fourth element)
for i in g:
print (i, end=" ") #Output: 4 5 6 7 8 9 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment