Python Programming · Middle Stage (6–8) · 3 courses, 34 worked problems, 69 practice questions
Computers repeat steps quickly and without mistakes. A loop tells Python to run a block of code again and again. A for loop repeats once for each item in a list, or for each number from range(), so for i in range(5): runs five times. A while loop repeats as long as a condition is True, which suits cases where you do not know the count in advance. The word break stops a loop early, and continue skips to the next turn. Loops make tables, add up totals and run step-by-step simulations.
for loop over a list or a range()while loop that stops when a condition failsChoose an answer, then check it. Each one shows a full explanation.
A for loop walks through a string and prints each character twice.
for ch in "abc":
print(ch * 2, end="-")
print()
What does the program print?
A while loop walks through a word with an index. After it finds the letter a, it jumps two places ahead.
s = "aardvark"
i = 0
found = 0
while i < len(s):
if s[i] == "a":
found += 1
i += 2
else:
i += 1
print(found, i)
What is printed?
Two nested counting loops add up numbers. The inner range starts at the outer variable, so the inner loop gets shorter each time round.
total = 0
for i in range(1, 4):
for j in range(i, 4):
total += j
print(total)
What does the program print?
A loop counts vowels in a short phrase. It skips spaces, it can stop early, and it has an else block.
count = 0
for ch in "sky is blue":
if ch == " ":
continue
if ch in "aeiou":
count += 1
if ch == "u":
break
else:
count = 100
print(count)
What does the program print?
Starting from ten, the program halves the number when it is even, and triples it and adds one when it is odd, counting the steps until it reaches one. The while statement has an else part.
n = 10
steps = 0
while n != 1:
if n % 2 == 0:
n //= 2
else:
n = 3 * n + 1
steps += 1
else:
print("done", end=" ")
print(steps)
What does the program print?
Two loops build a list of two-digit codes. The inner loop uses both continue and break.
codes = []
for i in range(3):
for j in range(3):
if j == i:
continue
if j > i + 1:
break
codes.append(str(i) + str(j))
print(codes)
What does the program print?
The program should spell a word backwards, one letter at a time. The slice in the loop header is missing, shown as ____.
word = "loops"
result = ""
for ch in ____:
result += ch
print(result)
Which slices complete the program so that its output is spool? There may be more than one.
One or more options are correct. Tick all the correct ones: a wrong option costs marks, and a part-right answer earns part of the marks.
The first line typed says how many words follow. The user types 3, then pear, fig and plum, one on each line, and the program keeps the longest word.
n = int(input())
longest = ""
for i in range(n):
word = input()
if len(word) >= len(longest):
longest = word
print(longest, n)
What is printed?
A program searches a list of tuples for a number whose double is 5. If the search fails, it reports the last tuple it saw.
pairs = [(1, "a"), (2, "b"), (3, "c")]
for n, ch in pairs:
if n * 2 == 5:
print("found", ch)
break
else:
print("none", n, ch)
What does the program print?
A shop keeps its stock in a dictionary. A loop lists the items that are running low. stock = {"pen": 0, "ink": 4, "pad": 1} need = [] for item, qty in stock.items(): if qty > 1: continue need.append(item) print(need, len(stock)) What does the program print?
Each one has a hint, the key idea and a full worked solution.