Python Quest

Learning map · Track 4 · beginner

Loops and Iteration

Most useful programs do the same job many times: total every line of a receipt, check every sensor reading, keep asking until the answer makes sense. This track teaches you to write that repetition with Python's for and while loops. You'll start with counting loops over range(), move on to looping over text and lists, and learn the patterns that turn a loop into an answer — running totals, counters, built-up strings, the largest or smallest value, the first match, and all-of or any-of checks with flags.

It is written for learners who already know print(), variables, types, f-strings, input() and if statements. Along the way you'll read input until a stop word, validate what users type, leave loops early with break and continue, work through grids with nested loops, pair data with enumerate() and zip(), and learn to recognise, stop and fix loops that never end. The track finishes with a mini project: a sales tally built in five milestones, using every loop pattern you've learned.

By the end of this track you can

  • Write for loops over range(), strings and lists, choosing start, stop and step without off-by-one errors
  • Use accumulators and counters to compute totals, counts, averages and built-up strings, including for empty input
  • Write while loops, sentinel loops and validation loops that always terminate, and diagnose ones that don't
  • Control loops with break, continue and a loop else, and combine loops with enumerate(), zip() and nesting
  • Find maximums, minimums, first matches, streaks and all-of/any-of answers by hand with a loop
  • Trace loop state with trace tables and debug prints to find and fix loop bugs

Lessons

  1. 1Why loops? Repeating work with forUse a for loop over range() to repeat a block of code a set number of times, and use the loop variable inside it.25 min · 3 exFree
  2. 2range() in depth: start, stop and stepControl exactly which numbers a for loop visits with range(start, stop, step), count down, and avoid off-by-one errors.30 min · 4 exMembers
  3. 3Looping over text and listsUse a for loop to visit every character of a string and every item of a simple list, without counting positions yourself.30 min · 4 exMembers
  4. 4Accumulators: totals, counts and built-up stringsCombine values across iterations with the accumulator pattern: running totals, counters, averages and strings built piece by piece.35 min · 4 exMembers
  5. 5while loops and loops that never endRepeat while a condition stays true, choose between while and for, and recognise, stop and fix infinite loops.40 min · 4 exMembers
  6. 6Input loops and sentinel valuesKeep reading input until the user types a stop word, and re-ask for input until it is valid.35 min · 4 exMembers
  7. 7break, continue and the loop elseLeave a loop early with break, skip the rest of one iteration with continue, and use a loop's else clause to handle 'not found'.35 min · 4 exMembers
  8. 8Nested loops: grids, tables and pairsPut one loop inside another to work through rows and columns, build tables, list every pair, and understand how the work grows.40 min · 4 exMembers
  9. 9enumerate() and zip(): positions and parallel dataGet each item together with its position using enumerate(), and walk through two sequences side by side with zip().35 min · 4 exMembers
  10. 10Finding patterns: smallest, largest, first and allWrite the loops behind min, max, search, all and any yourself: track the best value so far, stop at the first match, and use flags.40 min · 4 exMembers
  11. 11Mini project: a sales tallyBuild a till-side sales tally step by step — totals, skipped entries, the largest sale, a bar chart and a multi-day report — while tracing loop state to find bugs.60 min · 5 exMembers