Here is an A Level Computer Science worksheet on the topic “Nested for Loops in Python”.
It
contains 15 questions, each followed by an answer and explanation in non-tabular format.
Worksheet: Nested for Loops in Python (A Level CS 9618)
1. Write a nested for loop to print a 3x4 grid of asterisks.
for i in range(3):
for j in range(4):
print("*", end=" ")
print()
Explanation:
The outer loop runs 3 times (rows), and the inner loop runs 4 times (columns). end=" " keeps
the asterisks on the same line. print() moves to the next line after each row.
2. Modify the above code to print numbers 1 to 12 in a 3x4 grid.
count = 1
for i in range(3):
for j in range(4):
print(count, end=" ")
count += 1
print()
Explanation:
A counter variable count starts at 1 and increments in each iteration of the inner loop, producing
12 numbers arranged in 3 rows and 4 columns.
3. Print the following pattern using nested for loops:
1
1 2
1 2 3
1 2 3 4
for i in range(1, 5):
for j in range(1, i + 1):
print(j, end=" ")
print()
Explanation:
The outer loop defines the number of rows, and the inner loop prints increasing values from 1 up
to the current row number.
4. Create a 5x5 multiplication table using nested for loops.
for i in range(1, 6):
for j in range(1, 6):
print(i * j, end="\t")
print()
Explanation:
Each cell displays i * j. Using \t aligns the output neatly in columns.
5. How many times will this code print “Hello”?
for i in range(3):
for j in range(2):
print("Hello")
Answer: 6 times
Explanation:
Outer loop runs 3 times, inner loop runs 2 times => 3 × 2 = 6 iterations.
6. Print this pattern using a nested loop:
* * * *
* * *
* *
*
for i in range(4, 0, -1):
for j in range(i):
print("*", end=" ")
print()
Explanation:
The outer loop counts down from 4 to 1. The inner loop prints stars equal to the current row
number.
7. What is the output of this code?
for i in range(1, 4):
for j in range(1, 3):
print(i + j, end=" ")
print()
Answer:
2 3
3 4
4 5
Explanation:
Each iteration prints i + j. The outer loop controls the rows, inner loop adds values.
8. Write a program using nested loops to generate this output:
1
2 4
3 6 9
4 8 12 16
for i in range(1, 5):
for j in range(1, i + 1):
print(i * j, end=" ")
print()
Explanation:
Each number is the product of the current row and column number.
9. How many total iterations happen in this nested loop?
for i in range(4):
for j in range(3):
pass
Answer: 12
Explanation:
Outer loop: 4 times, Inner loop: 3 times per outer iteration → 4 × 3 = 12 total iterations.
10. Write a nested loop to print the following:
A
A B
A B C
for i in range(1, 4):
for j in range(65, 65 + i):
print(chr(j), end=" ")
print()
Explanation:
chr(65) is 'A', chr(66) is 'B', etc. Looping over ASCII values and converting to characters
using chr().
11. What will this code output?
for i in range(1, 3):
for j in range(1, 4):
print(f"{i},{j}", end=" ")
print()
Answer:
1,1 1,2 1,3
2,1 2,2 2,3
Explanation:
Each pair of (i,j) is printed as coordinates using f-strings.
12. Write a nested loop to print even numbers from 2 to 10 in two rows.
num = 2
for i in range(2):
for j in range(3):
print(num, end=" ")
num += 2
print()
Explanation:
Even numbers increase by 2. Two rows of 3 numbers each = 6 even numbers.
13. Print a right-angled triangle of letters like this:
a
a b
a b c
for i in range(1, 4):
for j in range(97, 97 + i):
print(chr(j), end=" ")
print()
Explanation:
ASCII 97 is 'a'. We loop and convert ASCII to letters.
14. Predict the output of this nested loop:
for i in range(3, 0, -1):
for j in range(i):
print(i, end=" ")
print()
Answer:
3 3 3
2 2
1
Explanation:
Each row prints the current i value i times.
15. Write a program that prints a pattern of odd numbers like this:
1
3 5
7 9 11
odd = 1
for i in range(1, 4):
for j in range(i):
print(odd, end=" ")
odd += 2
print()
Explanation:
Starts from 1 and increases by 2 (odd numbers), with increasing numbers per row.
Let me know if you'd like this converted to a printable format (PDF/Word) or want an extension
worksheet with structured 4-mark questions.