for loops
Module 1 — What is one for-loop?
Go through each location in a collection once.
Definition
One for-Small goes through the elements of a
iterated object - e.g. a list, a string or a
range()-Area — and executes the same block of code once for each element.
In contrast to the while-Loop, you don't have to worry about counting variables and termination yourself: As soon as the last element has been processed, the loop ends automatically.
Syntax:
for <variable> in <sammlung>:
<Anweisungen>
The Running variable takes the value of the next element in each pass.
There are two types of parts in the exercises: In most of them you read values
input() (as introduced in the “Variables” module) and the check automatically feeds in the entries. In others you define the collection yourself
(e.g. your own list) and follow along for above that - then nothing will be read in. The type of part is stated in the exercisesposition.
Example 1.1 — walking through a list
Loading Python…Tests
Module 2 — Numbers with range()
Three forms: range(stop), range(start, stop), range(start, stop, step).
range(stop)
range(5) generates the numbers 0, 1, 2, 3, 4. Important: the upper end is exclusive, d. h. the number 5 itself no longer appears.
Example 2.1 — range(5)
Loading Python…Tests
range(start, stop)
You specify start and stop with two arguments: range(1, 6) generated
1, 2, 3, 4, 5. So if you want “from 1 to 5,” you write range(1, 6).
Example 2.2 — range(1, 6)
Loading Python…Tests
range(start, stop, step)
You can also set the step size with three arguments. range(2, 11, 2) returns the even numbers 2, 4, 6, 8, 10. Negative increments are allowed, then it runs range backwards (range(10, 0, -1)).
Example 2.3 — range(2, 11, 2)
Loading Python…Tests
Module 3 — Iterate over strings & Rechargeable battery
Collect values, count letters.
Strings are iterable
One forloop over a string Letter by letter. You don't need an index - the running variable already contains the respective character.
Example 3.1 — walk through a string
Loading Python…Tests
Rechargeable batteries
Very often you need a variable that outside the loop with 0
or "" starts and is gradually expanded in the block - typical cases: sum, number, longest entry.
When counting letters is buchstabe == "a" case sensitive: In "Banane" comes the little one a exactly 2 times before (the big one B is not a).
Example 3.2 — Sum of a list
Loading Python…Tests
Example 3.3 — Counting letters in a word
Loading Python…Tests
Module 4 — for with if combination
Check each element and treat it differently.
Identification
Of course, you can use any other statements in the loop block — especially
if/else. This is how you filter, categorize or log each element.
A typical case is checking for even/odd numbers using the modulo operator:
zahl % 2 == 0 is true if and only if zahl is even.
Example 4.1 — even or odd?
Loading Python…Tests
TipSeparate collection and loop
Put lists, strings and range()-expressions if possible before the loop and give them descriptive names. In the loop head for x in xs: then it becomes clear at a glance, what you go through.
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python Language Reference — The
forstatement: docs.python.org/3/reference/compound_stmts.html#the-for-statement - Python tutorial — 4. More Control Flow Tools (Sections 4.2
forand 4.3range()): docs.python.org/3/tutorial/controlflow.html - Built-in Functions —
range(): docs.python.org/3/library/functions.html#func-range
Tutorials & Manuals
- Real Python — Python for Loops (Definite Iteration): realpython.com/python-for-loop
- Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 - Chapter on grinding.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — “Grinding” section.