Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

itertools.count

Produces an unbounded arithmetic progression from a starting point and step.

Version: Python 3.14; repository image, production runtime not attested

Syntax

itertools.count(start=0, step=1)

Produces an unbounded arithmetic progression from a starting point and step.

A zero step repeats the same value indefinitely. Bound the consumer before collecting results.

The first value is start, then step is repeatedly added. Defaults give 0, 1, 2, ... . With start 4 and step −2 the four values requested by islice are 4, 2, 0 and −2. Step zero repeats 7; without a bound neither iterator would terminate.

import itertools as I
assert list(I.islice(I.count(), 4)) == [0, 1, 2, 3]
assert list(I.islice(I.count(4, -2), 4)) == [4, 2, 0, -2]

Examples

import itertools as I
print(list(I.islice(I.count(4, -2), 4)))
print(list(I.islice(I.count(7, 0), 3)))

Result

[4, 2, 0, -2]
[7, 7, 7]

The first value is start, then step is repeatedly added. Defaults give 0, 1, 2, ... . With start 4 and step −2 the four values requested by islice are 4, 2, 0 and −2. Step zero repeats 7; without a bound neither iterator would terminate.

Sources