Vidyalelo
Python · Q20

Generators and Iterators

Programming · Python · question 20

Q20

How can you create an infinite generator in Python?

A.
By using a for loop
B.
By using the iter() function
C.
By using the while loop
Answer
D.
By using the infinity() function

Answer: Option C

Solution

Answer: Option C
Solution:
The correct answer is Option C: By using the while loop.
You can create an infinite generator in Python by using a while loop. Inside the while loop, you can use the yield statement to yield values infinitely. Since a while loop can continue indefinitely as long as its condition remains true, you can use it to create a generator that produces an infinite sequence of values. This allows you to generate values lazily, one at a time, without having to predefine the length of the sequence.