The itertools module in the standard library provides a number of tools for working with iterables.
The count()
function in the module creates an iterator which yields infinite sequence of integers from a starting point and incrementing by the specified amount with each step.
count(start = 0, step = 1)
copy
start |
The starting value. It defaults to 0. |
step |
The value to be used for incrementing after each iteration. It defaults to 1. |
The function returns an iterator which generates an infinite sequence of integers starting from the 'start
' value and incrementing by the 'step
' value with each iteration.
Using the count()
function is more efficient and fast than using range()
to generate the numbers. This is because count()
function doesn't have to store all the numbers in memory, instead it generates them one at a time as needed.