Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Generators - counter with parameter

def counter(n = 1):
    while True:
        yield n
        n += 1

for c in counter():
    print(c)
    if c >= 4:
        break
print()

for c in counter(8):
    print(c)
    if c >= 12:
        break

Output:

1
2
3
4

8
9
10
11
12