← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 6 · Module overview

Week 6 — Loops II: for, range & Nested Loops

Introduction to Computer Science Free sample edition

Welcome to your Week 6 Start Here page.

Why this matters

A nursing student charting a reading every hour of a shift and a business major printing a price table row by row both repeat a step a known number of times, and both need the count to stop exactly where it should.

In plain language

Last week your programs learned to repeat with a while loop, and you had to set up a counter and remember to move it every time. This week you meet a loop that does that bookkeeping for you. A for loop (a loop that takes the items of a sequence one at a time) walks through a string letter by letter, or through a range (a row of whole numbers Python makes on request) number by number. You will learn exactly where a range stops, how to total numbers with it, and how to put one loop inside another to print grids and tables. As always, you predict what each program prints and then run it to check.

  • Already covered: Week 5's while loop, counters, accumulators, and the off-by-one error.
  • This week: the for loop, range() and its exclusive stop, totals, end, and nested loops.
  • How they connect: a for loop counts for you, and its stop works like a slice's.
  • Where we're going next: Week 7, Functions.

Focus

How do you repeat a step a known number of times, and know exactly where the count stops?

By the end of this module, you should be able to:

  • [ ] predict which numbers a range produces
  • [ ] total numbers with a for loop and range(1, n + 1)
  • [ ] count the lines a nested loop prints

To-do

Plan your week

  1. Read Chapter 6 (about 40 minutes), then the lecture and slides.
  2. Work the lecture tutorial (60–90 minutes) and the practice set (about 20 minutes).
  3. Do the Coding Lab (about 90 minutes), the discussion, and the assignment.
  4. Take the quiz.

This week's checkpoint: predict the numbers a range produces, run it with list(), and say which number it left out.

You've got this

Predict, then run, and ask your instructor if a step snags.

Built with The Course Maker · samples.thecoursemaker.com