Chapter 12: Loops
Complete Python lesson for very beginners. Technical words are explained in simple language, and every outline topic includes a practical example, expected output, steps, and practice.
Chapter Overview
This Python tutorial chapter covers Loops through 15 connected topics. Work through the examples in order, check the expected output, and complete the practice after each topic.
- 12.1 Iteration
- 12.2 while Loops
- 12.3 for Loops
- 12.4 range()
- 12.5 Loop Variables
- Plus 10 additional Python topics in this chapter.
12.1 Iteration
Iteration is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for color in ["red", "blue"]:
print(color)Expected Output
red
blueStep-by-Step Explanation
- The list is iterable.
- for assigns each item to color in turn.
- The indented body runs once per item.
12.2 while Loops
while Loops is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
count = 1
while count <= 3:
print(count)
count += 1Expected Output
1
2
3Step-by-Step Explanation
- Initialize a loop variable.
- Repeat while the condition is true.
- Update the variable so the loop can finish.
12.3 for Loops
for Loops is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for color in ["red", "blue"]:
print(color)Expected Output
red
blueStep-by-Step Explanation
- The list is iterable.
- for assigns each item to color in turn.
- The indented body runs once per item.
12.4 range()
range() is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(1, 4):
print(n)Expected Output
1
2
3Step-by-Step Explanation
- range(1, 4) produces 1, 2, 3.
- The stop value is excluded.
- for visits each generated integer.
12.5 Loop Variables
Loop Variables is part of Loops. In simple language, it means a name that refers to a value.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(3):
print(n)Expected Output
0
1
2Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
12.6 Nested Loops
Nested Loops is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(3):
print(n)Expected Output
0
1
2Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
12.7 break
break is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(5):
if n == 2:
break
print(n)Expected Output
0
1Step-by-Step Explanation
- Loop through numbers.
- break exits the nearest loop immediately.
- Values after the break point are not printed.
12.8 continue
continue is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(4):
if n == 2:
continue
print(n)Expected Output
0
1
3Step-by-Step Explanation
- continue skips the rest of the current iteration.
- The loop itself continues.
- Only 2 is skipped.
12.9 pass
pass is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(3):
print(n)Expected Output
0
1
2Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
12.10 Loop else
Loop else is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(3):
print(n)Expected Output
0
1
2Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
12.11 Infinite Loops
Infinite Loops is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(3):
print(n)Expected Output
0
1
2Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
12.12 Iterating Strings
Iterating Strings is part of Loops. In simple language, it means text stored as characters.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(3):
print(n)Expected Output
0
1
2Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
12.13 Iterating Collections
Iterating Collections is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(3):
print(n)Expected Output
0
1
2Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
12.14 Common Loop Patterns
Common Loop Patterns is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
import re
text = "Order 123"
match = re.search(r"\d+", text)
print(match.group() if match else "none")Expected Output
123Step-by-Step Explanation
- Import re.
- The raw-string pattern \d+ means one or more digits.
- search() finds the first matching region.
12.15 Loop Projects
Loop Projects is part of Loops. In simple language, it means a Python idea used while learning loops.
It is one building block of loops and helps you write clearer, more predictable Python programs. For a very beginner, focus first on what goes in, what Python does, and what comes out; details become easier after you run a small example.
Python / Practical Example
for n in range(3):
print(n)Expected Output
0
1
2Step-by-Step Explanation
- Read the example from top to bottom.
- Identify the value, object, or operation related to this topic.
- Change one small input and predict the result before running it.
Common Beginner Mistakes
- Copying code without predicting what each line does.
- Ignoring the first useful error message or traceback location.
- Mixing tabs/spaces or changing indentation accidentally.
- Using data of the wrong type for an operation.
- Trying to learn many advanced variations before mastering one small working example.
Chapter Practice
- Choose three topics from this chapter and re-type their examples without copying and pasting.
- For each example, change one input and predict the output first.
- Explain five technical terms from this chapter in your own beginner-friendly words.
- Create one small program that combines at least two chapter topics.
- Keep notes about errors you made and what fixed them.
Mini Project / Challenge
Create a small Python exercise that combines at least three ideas from Loops. Start with a tiny working version, test it, then improve it one step at a time.
- Choose three topics from this chapter.
- Write or adapt a small Python example using those topics.
- Predict the output before running the code.
- Test at least one different input.
- Write two sentences explaining what the program does and what you learned.
20 Questions & Answers
1. What is the main goal of Chapter 12?
The goal is to understand loops through small explanations, examples, and practice.
2. Should I memorize every command or method?
No. Understand the pattern, practice the common form, and learn how to read documentation when you need exact details.
3. Why are the examples small?
Small examples isolate one idea at a time, which makes errors easier to understand and fix.
4. What should I do when an example gives an error?
Read the last part of the traceback, check spelling and indentation, confirm the required package or file exists, and compare the input types with what the operation expects.
5. Why should I predict output before running code?
Prediction forces you to reason about the program instead of only copying it.
6. What should I remember about Iteration?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
7. What should I remember about while Loops?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
8. What should I remember about for Loops?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
9. What should I remember about range()?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
10. What should I remember about Loop Variables?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
11. What should I remember about Nested Loops?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
12. What should I remember about break?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
13. What should I remember about continue?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
14. What should I remember about pass?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
15. What should I remember about Loop else?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
16. What should I remember about Infinite Loops?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
17. What should I remember about Iterating Strings?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
18. What should I remember about Iterating Collections?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
19. What should I remember about Common Loop Patterns?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.
20. What should I remember about Loop Projects?
Remember its beginner meaning, the problem it helps solve, the shape of a small example, and one common situation where it is appropriate.