Get AI summaries of any video or article — Sign up free
Python Tutorial for Beginners 7: Loops and Iterations - For/While Loops thumbnail

Python Tutorial for Beginners 7: Loops and Iterations - For/While Loops

Corey Schafer·
5 min read

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.

TL;DR

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?

break stops the loop entirely. In a for loop searching for a value (e.g., when num == 3), the code prints “found” and then exits, so later values (like 4 and 5) never run. continue skips only the current iteration. When num == 3 triggers continue, the loop moves directly to the next iteration (4, then 5) without executing the remaining statements for that iteration.

Why does nested looping produce combinations, and what’s the main risk?

An outer loop (over numbers) repeats its body for each number, and an inner loop (over characters in a string like “ABC”) runs once per outer-loop iteration. That structure prints every pairing of number and character (1 with a/b/c, then 2 with a/b/c, etc.). The risk is growth: combinations multiply quickly, so large lists or long strings can make the loop run for a long time.

What does range(10) produce, and how do you shift the start value?

range(10) generates integers from 0 up to but not including 10, so it yields 0–9 (10 values total). To start at 1 and still include 10, use range(1, 11): it begins at 1 and stops before 11, producing 1–10.

What makes a while loop stop, and how can it fail?

A while loop stops when its condition becomes false. For example, while x < 10 prints x and then increments x by 1; once x reaches 10, the condition (10 < 10) is false and the loop ends. It fails if x never changes (or changes the wrong way), because the condition stays true and the loop becomes infinite.

How do infinite loops work in Python, and how do you exit them safely?

An infinite loop can be created with while True, which keeps the condition permanently true. To stop it, code must include break when a certain input/value is received. If an infinite loop runs unintentionally, most terminals can interrupt execution with Ctrl+C (a keyboard interrupt).

Review Questions

  1. In a for loop, what is the difference in output between using break and using continue when num equals a target value?
  2. Given while x < N: print(x); x += 1, what values will print, and what happens if x += 1 is removed?
  3. 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. 1

    Use for loops to iterate over each element in a sequence, assigning the loop variable to each item in order.

  2. 2

    Use break to exit a loop immediately once a condition is met, preventing any further iterations.

  3. 3

    Use continue to skip only the current iteration and move on to the next cycle without running the remaining loop body.

  4. 4

    Nested loops generate combinations of outer-loop and inner-loop values, but iteration counts can grow quickly.

  5. 5

    Use range for fixed-count repetition; remember the end value is excluded (range(10) → 0–9).

  6. 6

    Use while loops for condition-driven repetition, and update the controlling variable so the condition eventually becomes false.

  7. 7

    Create infinite loops with while True only when break will eventually stop them; otherwise interrupt with Ctrl+C.

Highlights

break ends the loop completely, so later values never get processed.
continue skips the current iteration, so the loop keeps running but avoids executing the rest of the body for that value.
Nested loops over numbers and characters generate every number–character pairing, and the total work multiplies fast.
range(10) produces 0–9 because the stop value is excluded.
while True creates an infinite loop; Ctrl+C is the typical emergency stop in terminals.

Topics

  • For Loops
  • While Loops
  • Break and Continue
  • Nested Loops
  • range Function

Mentioned