What this topic tests
- Predict how many times range and while loops run.
- Build a total or a filtered list one step at a time.
- Use break and continue deliberately, not as a substitute for a clear condition.
Common mistakes
- Assuming range(1, 5) includes 5. The stop value is excluded.
- Forgetting to update the variable used by a while condition.
- Changing a list while looping over it and skipping elements.
Sample questions
Ten representative questions from the full bank. Try each one before opening the answer.
-
1. Ming sends coupons to tables 2 through 5. Which table numbers does range(2, 6) produce?
Show answer
Answer
B. 2,3,4,5Why: range(start, stop) includes start and excludes stop, so 2, 3, 4, 5. Reading stop as an inclusive bound is what produces the extra 6.
-
2. 🧋 What does range(10, 0, -1) produce?
Show answer
Answer
A. 10,9,8,...,1Why: A negative step counts down and the stop is still excluded, so 0 never appears; range(10, 0, -1) ends at 1. range(10, -1, -1) would include 0.
-
3. 🧋 How many pairs does zip([1,2,3], ['a','b']) produce?
Show answer
Answer
B. 2Why: zip stops as soon as the shortest iterable is exhausted, so the extra 3 is dropped without an error. zip_longest keeps it with a fill value.
-
4. To process each order in a list in turn, which loop is most direct?
Show answer
Answer
C. for order in ordersWhy: A for loop walks the iterable directly and stops when it is exhausted. while True would need a manual index, an exit test and a break.
-
5. Ming pairs drink names with prices. Which function iterates two lists together: ___(names, prices)?
Show answer
Answer
zipWhy: zip walks two iterables in step and yields one tuple per position, which the loop unpacks. It stops at the shorter input, so unequal lengths silently drop data.
-
6. 🧋 How many elements does range(3) produce?
Show answer
Answer
3Why: range(3) yields 0, 1, 2: three values starting at 0. Beginners often answer 2 by counting 1 to 3 instead of counting from 0.
-
7. Which built-in function produces an integer sequence for a for loop?
Show answer
Answer
rangeWhy: range is the built-in that produces an arithmetic sequence lazily; it does not build a list in memory. Wrapping it in list() materialises the values.
-
8. What does this code print when the range is converted to a list?
print(list(range(2, 6)))Show answer
Answer
[2, 3, 4, 5]Why: list() materialises every value the range would yield; 6 is the stop and never appears. range itself is lazy, so printing it directly shows range(2, 6).
-
9. A sign prints the letters of "go" in uppercase without spaces. What appears?
for ch in "go": print(ch.upper(), end="")Show answer
Answer
GOWhy: Iterating a string yields characters, and upper() changes each one; end='' removes the newline so the letters join with no separator.
-
10. What does `factors(12)` return?
def factors(value): factor_list = [] divisor = 2 while value > 1: while value % divisor == 0: factor_list.append(divisor) value /= divisor divisor += 1 return factor_list print(factors(12))Show answer
Answer
[2, 2, 3]Why: 12 factors into 2, 2, 3; the inner loop exhausts 2 before divisor moves to 3. The order is ascending because divisor only increases.
Common questions
When should I use while instead of for?
Use for when you are iterating over a known collection or range. Use while when the stopping condition is the main thing you know.
Does break skip the else block?
Yes. A loop's else block runs only when the loop finishes without break.
Keep the streak going.
The full bank keeps your place in this browser, filters by topic, and includes a daily question and mistake review.