Lesson 02
Repeating actions and working with collections
Recap
Today
Motivation
A program executes the same block of code multiple times, while a condition holds or until it iterates through an entire collection.
Topic 14
Repeats a block of code while a condition is true
Condition → block → back to condition. When the condition becomes false, the loop ends.
Step by step
Each iteration: check condition → execute block → return
| i | i ≤ 5 | |
|---|---|---|
| 1 | True | 1 |
| 2 | True | 2 |
| 3 | True | 3 |
| 4 | True | 4 |
| 5 | True | 5 |
| 6 | False | exit |
Example
An accumulator variable collects the result while the condition holds.
Warning
If the condition never becomes false, the program hangs
Interrupt execution with Ctrl + C in the terminal or the stop button in Jupyter.
Question
1 2 3 4 55 4 3 2 15 4 3 2 1 0Take a minute to think →
Topic 15
Repetition with a known number of iterations
The variable i takes values from the sequence one by one.
Step by step
range() yields values, the loop runs the body for each one
| Iteration | i | i × i |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 4 |
| 3 | 3 | 9 |
| 4 | 4 | 16 |
| 5 | 5 | 25 |
The loop ends automatically when the sequence is exhausted.
range()
Creates a sequence of numbers
| Call | Sequence |
|---|---|
| range(5) | 0, 1, 2, 3, 4 |
| range(1, 6) | 1, 2, 3, 4, 5 |
| range(0, 10, 2) | 0, 2, 4, 6, 8 |
| range(10, 0, -1) | 10, 9, 8, ..., 1 |
The end value is not included in the range. range(5) produces numbers from 0 to 4.
Example
Same result as the while-version, but shorter and without manual counter management.
Question
Take a minute to think →
Topic 16
Flow control inside a loop
Terminates the loop entirely. All remaining iterations are skipped.
Skips the rest of the current iteration and moves to the next one.
break
When i reaches 5, the loop breaks. Numbers 5–9 are not printed.
continue
Even numbers are skipped. The loop moves to the next iteration, bypassing print.
Topic 17
An ordered collection of elements
A list can contain elements of different types. Element order is preserved.
Access
Indexing starts from zero
Slicing
list[start:end:step] — end is not included
Slicing returns a new list. The original is unchanged.
Topic 18
| Method | Action |
|---|---|
| list.append(x) | Add element to the end |
| list.remove(x) | Remove first occurrence of x |
| list.pop() | Remove and return last element |
| list.sort() | Sort in place |
| list.reverse() | Reverse in place |
| len(list) | Length (this is a function) |
Question
[1, 2, 3][2, 3, 4][2, 3, 4, 5][1, 2, 3, 4]Take a minute to think →
Topic 19
for directly over a list, without indices
The variable fruit takes the value of each list element in turn.
Bonus
A string behaves like a sequence of characters
Indexing and slicing work the same way for strings as for lists.
Exercise 1
Hint: check evenness with i % 2 == 0.
Solution
Exercise 2
Hint: sum(list) and len(list) simplify this.
Solution
Practical exercises
10 tasks covering the lesson topics. Open in Jupyter or VS Code and work through them.
Summary
Functions and dictionaries - organizing code and key-value data structures
Telegram: @gokalqurt