Python Language Itertools Module itertools.count

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Introduction:

This simple function generates infinite series of numbers. For example...

for number in itertools.count():
    if number > 20:
        break
    print(number)

Note that we must break or it prints forever!

Output:

0
1
2
3
4
5
6
7
8
9
10

Arguments:

count() takes two arguments, start and step:

for number in itertools.count(start=10, step=4):
    print(number)
    if number > 20:
        break

Output:

10
14
18
22


Got any Python Language Question?