12.1 Introduction to Loops
Introduction to Loops is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Introduction to Loops
for number in [1, 2, 3]:
print(number)
Output
1
2
3
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.2 Why Loops Are Useful
Why Loops Are Useful is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Why Loops Are Useful
for _ in range(3):
print("Welcome!")
Output
Welcome!
Welcome!
Welcome!
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.3 The `while` Loop
The `while` Loop is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: The `while` Loop
count = 1
while count <= 3:
print(count)
count += 1
Output
1
2
3
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.4 Controlling a `while` Loop
Controlling a `while` Loop is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Controlling a `while` Loop
remaining = 3
while remaining > 0:
print(remaining)
remaining -= 1
Output
3
2
1
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.5 The `for` Loop
The `for` Loop is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: The `for` Loop
for color in ["red", "green", "blue"]:
print(color)
Output
red
green
blue
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.6 Looping Through Strings
Looping Through Strings is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Looping Through Strings
for letter in "Cat":
print(letter)
Output
C
a
t
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.7 Looping Through Lists
Looping Through Lists is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Looping Through Lists
for price in [5, 8, 12]:
print(price * 2)
Output
10
16
24
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.8 Looping Through Tuples
Looping Through Tuples is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Looping Through Tuples
for size in ("small", "medium", "large"):
print(size)
Output
small
medium
large
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.9 Looping Through Sets
Looping Through Sets is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Looping Through Sets
for number in sorted({6, 2, 4}):
print(number)
Output
2
4
6
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.10 Looping Through Dictionaries
Looping Through Dictionaries is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Looping Through Dictionaries
student = {"name": "Lina", "grade": 8}
for key, value in student.items():
print(key, value)
Output
name Lina
grade 8
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.11 The `range()` Function
The `range()` Function is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: The `range()` Function
for number in range(5):
print(number)
Output
0
1
2
3
4
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.12 Range Start, Stop, and Step
Range Start, Stop, and Step is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Range Start, Stop, and Step
for number in range(2, 11, 2):
print(number)
Output
2
4
6
8
10
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.13 The `break` Statement
The `break` Statement is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: The `break` Statement
for number in range(1, 6):
if number == 4:
break
print(number)
Output
1
2
3
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.14 The `continue` Statement
The `continue` Statement is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: The `continue` Statement
for number in range(1, 6):
if number == 3:
continue
print(number)
Output
1
2
4
5
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.15 The `pass` Statement
The `pass` Statement is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: The `pass` Statement
for number in range(3):
if number == 1:
pass
print(number)
Output
0
1
2
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.16 Loop `else` Blocks
Loop `else` Blocks is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Loop `else` Blocks
for number in [1, 2, 3]:
print(number)
else:
print("Loop completed")
Output
1
2
3
Loop completed
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.17 Nested Loops
Nested Loops is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Nested Loops
for row in range(1, 3):
for column in range(1, 3):
print(row, column)
Output
1 1
1 2
2 1
2 2
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.18 `enumerate()`
`enumerate()` is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: `enumerate()`
names = ["Ali", "Sara", "Omar"]
for position, name in enumerate(names, 1):
print(position, name)
Output
1 Ali
2 Sara
3 Omar
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.19 `zip()`
`zip()` is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: `zip()`
names = ["Ana", "Ben"]
scores = [90, 85]
for name, score in zip(names, scores):
print(name, score)
Output
Ana 90
Ben 85
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.20 Parallel Iteration
Parallel Iteration is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Parallel Iteration
products = ["Book", "Pen"]
prices = [12, 2]
for product, price in zip(products, prices):
print(product, price)
Output
Book 12
Pen 2
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.21 Looping Backward
Looping Backward is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Looping Backward
for number in range(5, 0, -1):
print(number)
Output
5
4
3
2
1
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.22 Creating Patterns
Creating Patterns is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Creating Patterns
for size in range(1, 5):
print("*" * size)
Output
*
**
***
****
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.23 Processing User Input
Processing User Input is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Processing User Input
answers = ["yes", "no", "quit"]
for answer in answers:
if answer == "quit":
print("Finished")
break
print(answer)
Output
yes
no
Finished
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.24 Avoiding Infinite Loops
Avoiding Infinite Loops is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Avoiding Infinite Loops
count = 1
while count <= 3:
print(count)
count += 1
Output
1
2
3
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.25 Loop Performance
Loop Performance is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Loop Performance
numbers = [1, 2, 3, 4]
total = 0
for number in numbers:
total += number
print(total)
Output
10
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.26 Practical Loop Applications
Practical Loop Applications is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Practical Loop Applications
prices = [10, 15, 5]
total = 0
for price in prices:
total += price
print("Total:", total)
Output
Total: 30
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.27 Chapter Practice Exercises
Chapter Practice Exercises is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Chapter Practice Exercises
numbers = [2, 4, 6]
for number in numbers:
print(number ** 2)
Output
4
16
36
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.
12.28 Chapter Mini Project
Chapter Mini Project is an important part of learning Python loops. This topic teaches beginners how repeated instructions work and how Python moves through values step by step. Understanding it helps you write shorter, clearer programs instead of copying the same code many times. Study the example carefully and notice the indentation, loop condition, current value, and stopping point.
Example: Chapter Mini Project
items = ["Book", "Pen", "Bag"]
for number, item in enumerate(items, 1):
print(f"{number}. {item}")
Output
1. Book
2. Pen
3. Bag
Output Explanation: Python runs the statements in the example in order. The loop processes the available values or continues while its condition is true. During each repetition, the indented code runs once. The displayed lines are the exact results produced by those repetitions, and the loop stops after all required work is complete.