+ 2
Sololearn Python Challenge question
Hi, I got this question today that I can't figure out t = (n for n in range(5)) if type(t) == tuple: print(len(t)) else: print(next(t)) 1) Why isn't the type tuple? It has round brackets. 2) What does next function do? Fyi the output of this is 0
2 Answers
0
1) It's a generator.
t = (n for n in range(5))
print(type(t))
will give you the following output: <class 'generator'>
2) next() function will return the next item in an iterator. Given that t contains (0,1,2,3,4), your next item is considered as 0.