Loops — Python Practice Worksheet
Loops repeat code while a condition is true (while) or iterate over a sequence (for). They eliminate repetitive code and are essential for processing collections and running simulations.
Python for loop: for i in range(5) runs i = 0,1,2,3,4. range(start, stop) excludes stop. while loops need a condition that eventually becomes false to avoid infinite loops.
Common patterns: accumulator (total += x), counter (count += 1), and finding max/min. Always check loop bounds and off-by-one errors.
Skills practiced
- Writing for and while loops
- Using range() correctly
- Tracing loop output
- Avoiding infinite loops
Practice Worksheet: Loops
Instructions: Solve each problem carefully. Show all work clearly. Write your final answer in the space provided or on a separate sheet as directed.
-
1.Write a
forloop that prints the numbers from 1 to 10, each on a new line. -
2.What is the output of the following code?
total = 0 for i in range(5): total += i print(total) -
3.Write a
whileloop that repeatedly asks the user to enter a number. The loop should stop when the user enters the number 0. For each number entered (except 0), print its square. -
4.How many times will the following loop execute?
count = 0 while count < 10: print("Hello") count += 2 -
5.Use a
forloop to calculate and print the sum of all even numbers between 1 and 20 (inclusive). -
6.What is the output of this code?
for i in range(3, 0, -1): print(i) -
7.Write a loop that prints the multiplication table for the number 7 (from 1 to 10). Each line should show the multiplication and result, e.g., “7 x 1 = 7”.
-
8.Which of the following loops will run infinitely? (Circle all that apply)
- A. (a)
- B. (b)
- C. (c)
- D. (d)
-
9.Write a
forloop that iterates over a listnames = ["Alice", "Bob", "Charlie"]and prints each name in uppercase. -
10.The following code is supposed to print the numbers 1 through 5, but it has a bug. Identify the bug and write the corrected code.
num = 1 while num <= 5: print(num)
Answer Key
-
1.
Final answer:
for i in range(1, 11): print(i) -
2.
The loop sums 0 + 1 + 2 + 3 + 4 = 10.Final answer: 10
-
3.
Final answer:
num = int(input("Enter a number: ")) while num != 0: print(num ** 2) num = int(input("Enter a number: ")) -
4.
The loop executes 5 times (count = 0, 2, 4, 6, 8).Final answer: 5
-
5.
Final answer:
total = 0 for num in range(2, 21, 2): total += num print(total)The sum is 110. -
6.
Final answer:
3 2 1
-
7.
Final answer:
for i in range(1, 11): print(f"7 x {i} = {7 * i}") -
8.
Final answer: Options (a) and (d) will run infinitely.
-
9.
Final answer:
names = ["Alice", "Bob", "Charlie"] for name in names: print(name.upper()) -
10.
Final answer: The bug is that
numis never incremented, so the loop runs infinitely. Corrected code:num = 1 while num <= 5: print(num) num += 1
Common mistakes to avoid
- Off-by-one errors with range boundaries
- Infinite while loops when condition never changes
- Modifying loop variable incorrectly
- Using for when while is clearer for unknown iterations
Generate your own like this
Want a fresh set of loops problems at your chosen difficulty? Use WorksheetSmith to compile a custom, print-ready PDF in seconds.
Last updated: 2026