Sharpen your iteration skills with 12 hands-on practice problems covering Python for loops, numerical stepping with range(), index tracking with enumerate(), and nested structures.
Structured Problem Set: Each challenge includes an interactive solution toggle, syntax-highlighted code, dedicated terminal output, and algorithmic notes. Compare definite iteration against indefinite while loops, manage early exits with break and continue, and run self-validating test assertions.
total = 0
for i in range(1, 11):
print(i, end=' ')
total += i
print(f'\nTotal Sum: {total}')
Terminal Output:
1 2 3 4 5 6 7 8 9 10
Total Sum: 55
word = 'Python'
for ch in word:
print(f'Char: {ch}')
Terminal Output:
Char: P
Char: y
Char: t
Char: h
Char: o
Char: n
frameworks = ['Django', 'Flask', 'FastAPI']
for idx, item in enumerate(frameworks, start=1):
print(f'{idx}. {item}')
Terminal Output:
1. Django
2. Flask
3. FastAPI
for val in range(2, 21, 2):
print(val, end=' ')
print()
for num in range(10, 0, -1):
print(num, end=' ')
print()
Terminal Output:
2 4 6 8 10 12 14 16 18 20
10 9 8 7 6 5 4 3 2 1
num = 5
factorial = 1
for i in range(1, num + 1):
factorial *= i
print(f'{num}! =', factorial)
Terminal Output:
5! = 120
table_num = 7
for i in range(1, 11):
print(f'{table_num} x {i:>2} = {table_num * i:>2}')
Terminal Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
items = [10, 45, 88, 32, 99]
target = 88
for pos, x in enumerate(items):
if x == target:
print(f'Found {target} at index {pos}')
break
Terminal Output:
Found 88 at index 2
for n in range(1, 16):
if n % 3 == 0:
continue
print(n, end=' ')
print()
Terminal Output:
1 2 4 5 7 8 10 11 13 14
candidate = 17
for d in range(2, int(candidate ** 0.5) + 1):
if candidate % d == 0:
print(f'{candidate} is composite')
break
else:
print(f'{candidate} is a prime number!')
Terminal Output:
17 is a prime number!
rows = 5
for r in range(1, rows + 1):
print('*' * r)
Terminal Output:
*
**
***
****
*****
orig = [[1, 2], [3, 4], [5, 6]]
transposed = []
for c in range(len(orig[0])):
new_row = []
for r in range(len(orig)):
new_row.append(orig[r][c])
transposed.append(new_row)
print('Transposed:', transposed)
Terminal Output:
Transposed: [[1, 3, 5], [2, 4, 6]]
n_terms = 8
fib = [0, 1]
for _ in range(2, n_terms):
fib.append(fib[-1] + fib[-2])
print(f'First {n_terms} Fibonacci numbers:', fib)
Terminal Output:
First 8 Fibonacci numbers: [0, 1, 1, 2, 3, 5, 8, 13]
Work through all 12 problems interactively in our companion Jupyter Notebook:
Practice interactive code cells for all 12 challenges and run self-validating assertions.
View on GitHub Run in Google ColabRun these unit checks directly in Python or your local terminal to verify loop mechanics across all challenges:
# Self-verifying unit test suite for for loop mechanics
# Test 1: Natural numbers sum
total = sum(range(1, 11))
assert total == 55
# Test 2: Stepped range evens
evens = list(range(2, 21, 2))
assert len(evens) == 10 and evens[0] == 2 and evens[-1] == 20
# Test 3: Factorial
fact = 1
for x in range(1, 6):
fact *= x
assert fact == 120
# Test 4: Matrix transpose
m = [[1, 2], [3, 4], [5, 6]]
t = [[m[r][c] for r in range(len(m))] for c in range(len(m[0]))]
assert t == [[1, 3, 5], [2, 4, 6]]
# Test 5: Fibonacci terms
fib = [0, 1]
for _ in range(2, 8):
fib.append(fib[-1] + fib[-2])
assert fib == [0, 1, 1, 2, 3, 5, 8, 13]
print("All 12 for loop question assertions successfully verified!")Terminal Output:
All 12 for loop question assertions successfully verified!
The else block executes only if the loop completes all iterations without encountering a break statement.
The stop parameter in range() is strictly exclusive. To include 10, use range(1, 11).
Use the built-in enumerate() function: for idx, val in enumerate(my_list):.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.