Python loops MCQ questions with answers
for, while, range, break and continue: count the passes before you check.
0 of 20 answered
Question 1 · Easy
What is the output of this code?
for i in range(1, 10, 3): print(i, end=" ")
Show the answer
Answer: B
Run this code in the editorrange(1, 10, 3)starts at 1 and adds 3 each time while the value stays below 10: 1, 4, 7. The next value, 10, is not included because the stop value is never part of a range.Question 2 · Easy
What is the output of this code?
for i in range(10, 0, -3): print(i, end=" ")
Show the answer
Answer: C
With a negative step,
Run this code in the editorrangecounts down from the start and stops before passing the stop value: 10, 7, 4, 1. The next value, -2, would be past 0, so the loop ends.Question 3 · Medium
What is the output of this code?
print(list(range(5, 1))) print(list(range(-1, -5, -1)))
Show the answer
Answer: A
Run this code in the editorrange(5, 1)uses the default step of +1, and counting up from 5 never gets below 1, so it is empty.range(-1, -5, -1)counts down from -1 and stops before -5.Question 4 · Easy
What is the output of this code?
n = 1 while n < 20: n = n * 3 print(n)
Show the answer
Answer: D
The loop multiplies by 3 while
Run this code in the editorn < 20: 1 becomes 3, then 9, then 27. The condition is checked only at the start of each pass, sonends at 27, the first value that is not below 20.Question 5 · Medium
What is the output of this code?
for n in range(1, 10): if n % 3 == 0: continue if n == 8: break print(n, end=" ")
Show the answer
Answer: C
Run this code in the editorcontinueskips the rest of the current pass, so multiples of 3 are not printed.breakends the whole loop as soon asnis 8, before 8 is printed.Question 6 · Medium
What is the output of this code?
for nums in ([3, 5, 8, 9], [1, 7]): for n in nums: if n % 2 == 0: print("even:", n) break else: print("all odd")
Show the answer
Answer: B
The
Run this code in the editorelsebelongs to the innerforloop, not to theif. It runs only when that loop ends withoutbreak: the first list breaks at 8, so itselseis skipped; the second list has no even number, soall oddis printed once.Question 7 · Easy
What is the output of this code?
for i in range(1, 4): for j in range(i): print("*", end="") print()
Show the answer
Answer: A
The inner loop runs
Run this code in the editoritimes, printing stars on one line, andprint()then ends the line.itakes the values 1, 2 and 3, giving rows of 1, 2 and 3 stars.Question 8 · Hard
What is the output of this code?
for i in range(3): for j in range(3): if j == i: break print(i, j)
Show the answer
Answer: D
Run this code in the editorbreakends only the inner loop. Wheniis 0 it breaks at once; wheniis 1 it prints1 0and breaks atj == 1; wheniis 2 it prints2 0and2 1before breaking.Question 9 · Easy
What is the output of this code?
fruits = ["apple", "mango", "kiwi"] for i, fruit in enumerate(fruits, start=1): print(i, fruit)
Show the answer
Answer: B
Run this code in the editorenumeratepairs each item with a counter, andstart=1makes the counter begin at 1 instead of 0. Unpacking intoi, fruitprints the two values separately, not as a tuple.Question 10 · Medium
What is the output of this code?
names = ["Ana", "Ben", "Cy"] scores = [90, 85] for name, score in zip(names, scores): print(name, score)
Show the answer
Answer: C
Run this code in the editorzipstops as soon as the shortest input runs out.scoreshas only two items, soCyis never paired and nothing is filled in for it.Question 11 · Medium
What is the output of this code?
total = 0 for i in range(1, 5): total += i print(i, total)
Show the answer
Answer: D
Run this code in the editorrange(1, 5)gives 1, 2, 3 and 4, sototalis 10. The loop variable keeps its last value after the loop ends, and that value is 4, not 5.Question 12 · Hard
What is the output of this code?
nums = [1, 2, 4, 5, 6] for n in nums: if n % 2 == 0: nums.remove(n) print(nums)
Show the answer
Answer: B
Removing an item shifts the later items left while the loop's position moves on, so the item after each removed one is skipped. After 2 is removed, 4 slides into its place and is never checked. Loop over a copy (
Run this code in the editornums[:]) instead.Question 13 · Easy
Which error does this code raise?
for i in range(0, 1, 0.25): print(i)
Show the answer
Answer: C, TypeError: 'float' object cannot be interpreted as an integer
Run this code in the editorrange()accepts only integers for its start, stop and step. A float step such as 0.25 raisesTypeError; to count in steps of 0.25, loop over integers and divide, for examplei / 4.Question 14 · Medium
Which error does this code raise?
for i in range(0): print("inside") print(i)
Show the answer
Answer: A, NameError: name 'i' is not defined
Run this code in the editorrange(0)is empty, so the loop body never runs andiis never assigned. A loop variable exists after the loop only if the loop ran at least once.Question 15 · Hard
Which error does this code raise?
stock = {"pen": 0, "ink": 4, "nib": 0} for item in stock: if stock[item] == 0: del stock[item]
Show the answer
Answer: D, RuntimeError: dictionary changed size during iteration
Deleting a key while looping over the same dictionary changes its size, and Python stops with
Run this code in the editorRuntimeError. Loop over a copy of the keys instead:for item in list(stock):.Question 16 · Easy
How many times does the body of
for i in range(2, 11, 2):run?Show the answer
Answer: B, 5
The range gives 2, 4, 6, 8 and 10: the stop value 11 is excluded, but 10 is below it, so it is included. That is five values, so the body runs five times.
Question 17 · Medium
When does the
elseblock of aforloop run?Show the answer
Answer: A, When the loop finishes without hitting break
A loop's
elseruns once, after the loop ends normally, including when there was nothing to loop over. It is skipped whenbreakends the loop, which makes it handy for "not found" cases.Question 18 · Medium
Inside
for i in range(3):, the loop body setsi = 10. What happens to the loop?Show the answer
Answer: C, It still runs with i = 0, 1 and 2
Each pass of a
forloop takes the next value from the range and assigns it toi, so changingiinside the body has no effect on the next pass. To control the count yourself, use awhileloop.Question 19 · Easy
Which loop prints 5, 4, 3, 2, 1, one number per line?
Show the answer
Answer: A
Counting down needs a negative step, and the stop value is excluded, so the stop must be 0 to include 1.
range(5, 1, -1)stops at 2, and the last two ranges are empty because their step points away from the stop.Question 20 · Hard
Which code removes every even number from
nums = [2, 4, 6, 7], leaving[7]?Show the answer
Answer: D
nums[:]is a copy, so removing items fromnumsdoes not disturb the loop. Looping overnumsitself skips items (it leaves[4, 7]), therangeversion runs past the shrinking list withIndexError, andpop()removes from the end.
Keep going
- Read the lesson behind these questions: Loops in the PythonBook
- Next topic: Python list MCQ questions with answers
- Before this: Python if-else MCQ questions with answers
- Write the code yourself in the Python coding challenges, which check your solution with tests.