What is the output of the following? “`python gen = (x * 2 for x in range(4)) print(next(gen)) print(next(gen)) “`

Python Developer Hard

Python Developer — Hard

What is the output of the following? “`python gen = (x * 2 for x in range(4)) print(next(gen)) print(next(gen)) “`

Key points

  • The generator expression `(x * 2 for x in range(4))` multiplies each value of `x` by 2.
  • `next(gen)` retrieves the next value from the generator.
  • The first call to `next(gen)` returns 0, and the second call returns 2.

Ready to go further?

Related questions