Python Tutorial for Beginners 7: Loops and Iterations - For/While Loops
Based on Corey Schafer's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Use for loops to iterate over each element in a sequence, assigning the loop variable to each item in order.
Briefing
Loops and iterations in Python come down to two core tools: for loops that step through values, and while loops that keep running until a condition changes. The practical difference matters because it determines how you control progress through data—and how you stop early when you’ve found what you need.
A for loop iterates over each item in a sequence. Given a list like nums = [1, 2, 3, 4, 5], the pattern for num in nums assigns num to each element in turn, letting code run once per item. Two keywords add control inside that loop. break exits the loop completely as soon as a condition is met, which is useful when searching a list: if num equals 3, the loop prints “found” and then stops, so values 4 and 5 never get processed. continue, by contrast, skips only the current iteration and moves on to the next one. Replacing break with continue means the loop still keeps going after printing “found,” but it avoids printing the current value (like 3) because the iteration is skipped to the next cycle.
Nested loops extend this idea by running one loop inside another. Using an outer loop over numbers and an inner loop over a string like “ABC” produces every combination of the two: for 1 it prints 1 a, 1 b, 1 c; then for 2 it prints 2 a, 2 b, 2 c; and so on. This combination growth can get expensive quickly—more values in either loop means more total iterations—so nested loops require caution when the input sizes are large.
When the goal is simply to repeat an action a fixed number of times, Python’s range function provides a clean way to generate iteration counts. range(10) yields 0 through 9, because the end value is excluded. If the loop should start at 1 and still run 10 times, range(1, 11) produces 1 through 10.
While loops shift control from “iterate over values” to “keep checking a condition.” A while loop continues as long as a condition remains true, such as while x < 10. To ensure it eventually stops, x must be incremented inside the loop; otherwise the condition never becomes false. break can also end a while loop early when a specific value appears (for example, breaking when x equals 5). For cases where repeated checking should continue indefinitely until some input arrives, an infinite loop can be created by using while True and relying on break to stop it. If such a loop runs away, most environments allow interruption with Ctrl+C, which sends a keyboard interrupt.
Together, for/while loops plus break/continue and range form the backbone of Python iteration: they let code process sequences, generate combinations, repeat tasks predictably, and stop safely when conditions are met—without wasting time on unnecessary iterations.
Cornell Notes
Python iteration centers on two loop types. A for loop walks through each element in a sequence (like a list), while a while loop keeps running until a condition becomes false. break exits the current loop immediately, which is ideal for searching—once the target is found, remaining items are skipped. continue skips only the current iteration, so the loop proceeds to the next value without performing the rest of the loop body for the skipped case. range helps repeat code a fixed number of times (range(10) gives 0–9; range(1, 11) gives 1–10). While loops require careful updates to the controlling variable to avoid infinite loops.
How do break and continue change what happens inside a loop when a condition is met?
Why does nested looping produce combinations, and what’s the main risk?
What does range(10) produce, and how do you shift the start value?
What makes a while loop stop, and how can it fail?
How do infinite loops work in Python, and how do you exit them safely?
Review Questions
- In a for loop, what is the difference in output between using break and using continue when num equals a target value?
- Given while x < N: print(x); x += 1, what values will print, and what happens if x += 1 is removed?
- How many iterations will a nested loop perform if the outer loop has A items and the inner loop has B items?
Key Points
- 1
Use for loops to iterate over each element in a sequence, assigning the loop variable to each item in order.
- 2
Use break to exit a loop immediately once a condition is met, preventing any further iterations.
- 3
Use continue to skip only the current iteration and move on to the next cycle without running the remaining loop body.
- 4
Nested loops generate combinations of outer-loop and inner-loop values, but iteration counts can grow quickly.
- 5
Use range for fixed-count repetition; remember the end value is excluded (range(10) → 0–9).
- 6
Use while loops for condition-driven repetition, and update the controlling variable so the condition eventually becomes false.
- 7
Create infinite loops with while True only when break will eventually stop them; otherwise interrupt with Ctrl+C.