Python (Fall 2026) Module 6-7: Introducing for Loops
Overview
Bill Moseley introduces Python for loops as a way to run the same indented code once for each item in a sequence, contrasting them with condition-driven while loops. He demonstrates iterating through lists and dictionary keys, using range(start, stop, step) for counted loops, and formatting dictionary key-value output with ljust and rjust.
Key takeaways
- A Python for loop processes each element of its iterable in order, assigning the current element to the loop variable before executing the body.
- Python range() excludes its stop value: range(0, 6) yields six integers, from 0 through 5.
- The three-argument form range(start, stop, step) supports increments and decrements, such as range(0, 30, 3) or range(10, 0, -1).
- Iterating directly over a dictionary yields its keys, and a key can retrieve its associated value with dictionary[key].
- Descriptive loop variables such as strName make clear what each iteration contains, while string methods such as upper(), ljust(), and rjust() can format output.
Chapters
- A while loop repeats while a Boolean condition remains true, such as incrementing x until it reaches 4.
- A for loop instead takes items from a sequence one at a time and runs its indented block for each item.
- Because the sequence determines how many items are processed, a for loop over a fixed list avoids accidentally repeating forever.
- Sequence elements can be numbers, fruit strings, or other values, and each is assigned to the loop variable for that iteration.
- range(0, 6) produces 0 through 5 because Python includes the starting value but excludes the stop value.
- range(6) is shorthand for starting at 0, while range(0, 30, 3) counts by threes through 27.
- A loop variable can use a descriptive name such as intNumber instead of x; range(0, 20, 4) ends at 16.
- To count down from 10 toward 0, use range(10, 0, -1); a negative step is required for descending ranges.
- For strName in liNames assigns each name, such as Bill, Sam, and Joe, to strName for one iteration.
- A loop can transform each string during output, for example converting names to lowercase or uppercase.
- Looping with for strName in diNames visits dictionary keys; diNames[strName] retrieves the corresponding phone number.
- Using ljust(10) for names and rjust(15) for phone numbers creates aligned output with fields totaling 25 characters.
Transcript excerpt
0:04 All right, welcome back everybody. Hope you had a nice weekend. Today we're going to pick up the second half of module six, which is for loops. If you remember, last time we talked about while loops, which were structured like this, and we start with the keyword while. We have an expression that evaluates true or false. If the expression is true, then it executes the indented block of code immediately beneath it. And we'll continue to do that as long as the expression is true. So in this case, obviously we have the expression is x is less than 4. When it starts out, x is one. So it prints it and then increments one. It does that three times. And then on the
0:56 fourth round it will not repeat itself, because at that point x will be equal to four. Again, if we look at this, that same code in a visual format. So we're setting x to one, and we're asking is x less, not, than one. It says, I'm going to start blinking again. We did so well last time. So it says, is x less than 11, different target number but same effect. As long as it's less than 11, it will print the value of x and then increase x by one, until x is no longer less than 11, at which point, I was starting to think it's this chord maybe is like shorting out. I guess I'll have to like be very gentle about touching my own computer here. So today we're going to go into
1:52 a different kind of loop. It's called a for loop. And a for loop operates a little bit differently. Most important thing is that it's not based on a condition, a true or false expression. Now it
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.