11-15-2025, 10:34 PM
Chapter 11 — Loops & Repetition
Loops let your program repeat actions without writing the same code over and over.
There are two main loops in Python:
• for loops — repeat a set number of times
• while loops — repeat until a condition changes
---
11.1 The for Loop
A for loop repeats a known number of times.
Example:
Output:
range(5) gives: 0, 1, 2, 3, 4
---
11.2 Loop Variable
Inside the loop, i changes each time:
Output:
You can rename it:
---
11.3 range() Variations
Start → End:
Output:
Start → End → Step:
Output:
---
11.4 Looping Through Strings
---
11.5 Looping Through Lists
---
11.6 The while Loop
Repeats while a condition is True.
---
11.7 Infinite Loops (Be Careful!)
❌ Warning:
You must break out manually.
---
11.8 Using break
Stops the loop immediately:
---
11.9 Using continue
Skip the current loop and continue with the next one:
Output:
---
11.10 Practical Pattern — Counting Down
---
11.11 Mini Project — Times Tables
Ask user for a number.
Print its times table up to 12.
---
11.12 Challenge — Password Attempt System
Rules:
• user gets 3 attempts
• password is “python123”
• use a while loop
• show attempts left
• break when successful
• lock them out after 3 fails
---
11.13 Chapter Summary
• for loops = repeat a set number of times
• while loops = repeat until condition changes
• range() controls the loop
• break stops early
• continue skips one loop
• loops work with lists, numbers, and strings
Next:
Chapter 12 — Lists: Storing Multiple Items
Lists are like containers that hold many values at once — essential for real programs.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
Loops let your program repeat actions without writing the same code over and over.
There are two main loops in Python:
• for loops — repeat a set number of times
• while loops — repeat until a condition changes
---
11.1 The for Loop
A for loop repeats a known number of times.
Example:
Code:
for i in range(5):
print("Hello")Output:
Code:
Hello
Hello
Hello
Hello
Hellorange(5) gives: 0, 1, 2, 3, 4
---
11.2 Loop Variable
Inside the loop, i changes each time:
Code:
for i in range(5):
print(i)Output:
Code:
0
1
2
3
4You can rename it:
Code:
for day in range(1, 8):
print("Day:", day)---
11.3 range() Variations
Start → End:
Code:
for x in range(3, 8):
print(x)Output:
Code:
3
4
5
6
7Start → End → Step:
Code:
for n in range(0, 20, 5):
print(n)Output:
Code:
0
5
10
15---
11.4 Looping Through Strings
Code:
for letter in "Python":
print(letter)---
11.5 Looping Through Lists
Code:
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)---
11.6 The while Loop
Repeats while a condition is True.
Code:
count = 1
while count <= 5:
print(count)
count += 1---
11.7 Infinite Loops (Be Careful!)
❌ Warning:
Code:
while True:
print("Forever")You must break out manually.
---
11.8 Using break
Stops the loop immediately:
Code:
while True:
word = input("Type 'stop' to end: ")
if word == "stop":
break---
11.9 Using continue
Skip the current loop and continue with the next one:
Code:
for n in range(1, 8):
if n == 4:
continue
print(n)Output:
Code:
1
2
3
5
6
7---
11.10 Practical Pattern — Counting Down
Code:
for n in range(10, 0, -1):
print(n)
print("Launch!")---
11.11 Mini Project — Times Tables
Ask user for a number.
Print its times table up to 12.
Code:
7 × 1 = 7
7 × 2 = 14
...
7 × 12 = 84---
11.12 Challenge — Password Attempt System
Rules:
• user gets 3 attempts
• password is “python123”
• use a while loop
• show attempts left
• break when successful
• lock them out after 3 fails
---
11.13 Chapter Summary
• for loops = repeat a set number of times
• while loops = repeat until condition changes
• range() controls the loop
• break stops early
• continue skips one loop
• loops work with lists, numbers, and strings
Next:
Chapter 12 — Lists: Storing Multiple Items
Lists are like containers that hold many values at once — essential for real programs.
---
Written and Compiled by Lee Johnston — Founder of The Lumin Archive
