Flow Control for-loop

The goal

For what reason…

Questions to David Rotermund

Logic blocks need to be indented.​ Preferable with 4 spaces!

The for statement

The for statement is used to iterate over the elements of a sequence (such as a string, tuple or list) or other iterable object

With range()

for i in range(0, 3):
    print(i)

Output:

0
1
2

range()

class range(stop)
class range(start, stop[, step])

With a list

for i in [0, "A", 7, "nom num"]:
    print(i)

Output:

0
A
7
nom num

The full statement

for_stmt ::=  "for" target_list "in" starred_list ":" suite
              ["else" ":" suite]

for loop (the truth)​

This is an optional topic!

The for loop is not counting up itself. It uses the __iter__ & __next__ combo of a instance of a class or a generator via yield.

Class

class ClassIterExample:
    counter: int
    counter_max: int

    def __init__(self):
        self.counter = 8
        self.counter_max = 12

    def __iter__(self):
        return self

    def __next__(self):
        if self.counter < self.counter_max:
            self.counter += 1
        else:
            raise StopIteration

        important_result: int = self.counter**2

        return f"Element: {self.counter}", important_result


test_instance = ClassIterExample()

for i in test_instance:
    print(i)

Output:

('Element: 9', 81)
('Element: 10', 100)
('Element: 11', 121)
('Element: 12', 144)

Generator yield

from typing import Generator


def do_more(min_value: int, max_value: int) -> Generator:
    for index in range(min_value, max_value):
        yield index**2


for i in do_more(min_value=3, max_value=8):
    print(i)

Output:

9
16
25
36
49

for loop – enumerate

enumerate(iterable, start=0)

If you need the index of the element then you can use enumerate:

for i in enumerate(range(10, 13)):
    print(i)

Output:

(0, 10)
(1, 11)
(2, 12)

This is an optional topic!

for i in enumerate(range(10, 13)):
    print(i)
else:
    print("*")
    print(i)

Output:

(0, 10)
(1, 11)
(2, 12)
*
(2, 12)

This is an optional topic!

class ClassIterExample:
    counter: int
    counter_max: int

    def __init__(self):
        self.counter = 8
        self.counter_max = 12

    def __iter__(self):
        return self

    def __next__(self):
        if self.counter < self.counter_max:
            self.counter += 1
        else:
            raise StopIteration

        important_result: int = self.counter**2

        return f"Element: {self.counter}", important_result


test_instance = ClassIterExample()

for i in enumerate(test_instance):
    print(i)

Output:

(0, ('Element: 9', 81))
(1, ('Element: 10', 100))
(2, ('Element: 11', 121))
(3, ('Element: 12', 144))

You see a tuple with the enumerate id as a first component and a second tuple as result from the iterator.

break

break may only occur syntactically nested in a for or while loop, but not nested in a function or class definition within that loop.

It terminates the nearest enclosing loop, skipping the optional else clause if the loop has one.

If a for loop is terminated by break, the loop control target keeps its current value.

break_stmt ::=  "break"
for i in range(0, 5):
    if i == 2:
        break
    print(i)

Output:

0
1

continue

continue may only occur syntactically nested in a for or while loop, but not nested in a function or class definition within that loop. It continues with the next cycle of the nearest enclosing loop.

continue_stmt ::=  "continue"
for i in range(0, 5):
    if i == 2:
        continue
    print(i)

Output:

0
1
3
4

The source code is Open Source and can be found on GitHub.