Python Programming - Generators - Discussion

Discussion Forum : Generators - General Questions (Q.No. 97)
97.
How does the itertools.count() function behave when used with a specified step value?
It raises a TypeError
It creates an infinite sequence of integers with the specified step value
It stops after reaching the specified step value
It raises a ValueError
Answer: Option
Explanation:
itertools.count() creates an infinite sequence of integers, and if a step value is specified, it increments each subsequent element by that step value.
import itertools

count_by_twos = itertools.count(start=0, step=2)

result = [next(count_by_twos) for _ in range(5)]
print(result)  # Output: [0, 2, 4, 6, 8]
Discussion:
Be the first person to comment on this question !

Post your comments here:

Your comments will be displayed after verification.