pytutorial/python_basics/for
David Rotermund 1fbc6fa48f
Create README.md
Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
2023-12-06 23:50:49 +01:00
..
README.md Create README.md 2023-12-06 23:50:49 +01:00

Flow Control for-loop

{:.no_toc}

* TOC {:toc}

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 (enumerate)

XXXXXXXXX

for loop (the truth)

{: .topic-optional} 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.

XXXXXXXXX