Skip to main content
Python Output Prediction Hard +35 XP

Generator Expression

What is the output?

PythonRead-only
gen = (x**2 for x in range(3))
print(next(gen))
print(next(gen))

What is the output?

Show answer and explanation

Answer: 0 then 1

Generators yield one value at a time. First next() gives 0**2=0, second gives 1**2=1.

Hint: Generators yield values one at a time.

What this challenge tests

Output Prediction in Python: generators, lazy-evaluation.

Related challenges