Language

Control Flow · Lesson 6 of 56

Loops

Source: 2-Control Flow/Loops.ipynb

Start here — no coding background needed

What you will learn

Repeat actions without copying the same lines many times.

In simple words

`for` repeats for each item in a list. `range(5)` gives 0,1,2,3,4 — useful for "do this 5 times".

Think of it like this

Checking off each name on an attendance sheet one by one.

Words to know:

  • for loop — Runs once per item in a collection
  • range() — Generates a sequence of numbers

Learn how programs make decisions and repeat tasks — like daily habits.

Easy example — try this first

Easy example — run this first. Change values and press Run again.

Python

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference notes (from full bootcamp)

Optional — deeper detail for when you are ready

Loops

Video Outline:

  1. Introduction to Loops
  2. for Loop

- Iterating over a range

- Iterating over a string

  1. while Loop
  2. Loop Control Statements

- break

- continue

- pass

  1. Nested Loops
  2. Practical Examples and Common Errors
Reference example
Python
Output
Expected (from notebook):
range(0, 5)

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
0
1
2
3
4

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
1
2
3
4
5

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
1
3
5
7
9

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
10
9
8
7
6
5
4
3
2

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
10
8
6
4
2

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
K
r
i
s
h
 
N
a
i
k

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
0
1
2
3
4

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
0
1
2
3
4

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
1
3
5
7
9

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
0
1
2
3
4

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
i:0 and j:0
i:0 and j:1
i:1 and j:0
i:1 and j:1
i:2 and j:0
i:2 and j:1

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
Sum of first 10 natural number: 55

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
55

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Reference example
Python
Output
Expected (from notebook):
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Runs in your browser via Pyodide — no server. First run may take a few seconds.

Conclusion:

Loops are powerful constructs in Python that allow you to execute a block of code multiple times. By understanding and using for and while loops, along with loop control statements like break, continue, and pass, you can handle a wide range of programming tasks efficiently.

Practice test — try yourself

Write code, press Check. Wrong answer shows the correct code to copy & run.

Print numbers 1 to 3, each on its own line (use a for loop).

Python