Skip to main content
Welcome back. Your code can already store information and make decisions. Loops let that code repeat actions reliably — whether ten times or a thousand — without duplicating statements. In this lesson you’ll learn how to use Python loops for automation, when to choose for vs while, and how to control loop flow. In this lesson you will learn to:
  • Use for loops to repeat an action a fixed number of times or iterate over collections.
  • Use while loops to repeat until a condition changes.
  • Track and update variables inside loops and use break to exit early.
  • Decide when to use for versus while loops in automation tasks.
Why loops matter
  • Reduce repetition and human error.
  • Make code scalable and easier to maintain.
  • Combine with data structures (lists, dicts, files) to process many items automatically.
Create a new file named loops.py. For loops — repeat a fixed number of times Without loops you would have to repeat statements manually. For example, printing numbers 0–10 with no loop would be tedious. A for loop does it succinctly:
Quick breakdown:
  • for and in define the loop header.
  • i is the loop variable (conventionally i, but any name works).
  • range(11) produces numbers from 0 up to, but not including, 11.
  • print(i) runs each iteration.
In many programming languages, counting often starts at zero. range(n) produces 0 through n-1 — this zero-based indexing aligns with arrays, lists, and memory layouts. Use range(start, stop, step) when you need custom start, end, or step values.
range(start, stop, step) options Example: simple ranges
Countdown (reverse range) with a pause between iterations To add a delay between loop iterations, import the time module and use time.sleep():
Multiplication table example Loops can do calculations each iteration. This example asks the user which times table to show and prints 1 through 10:
Example run (user enters 5):
Iterating over other iterables for loops work with many iterable types: lists, tuples, strings, dictionaries, file objects, and generator expressions. A couple of quick examples:
While loops — repeat until a condition changes Use a while loop when you don’t know how many iterations are required in advance — for example, waiting for valid user input or monitoring an external condition. Basic structure:
Password example:
Explanation:
  • while True: creates a loop that repeats until break is executed.
  • Each iteration asks the user for input. When the guess matches password, break exits the loop; otherwise it repeats.
Be careful with while loops — if the condition never becomes false (or you never break), the program runs forever. Ensure the loop updates state or receives input that can end the loop to avoid infinite loops.
Mood example A similar while pattern that accepts only “happy” or “sad”:
Controlling loop flow: break and continue
  • break exits the nearest loop immediately.
  • continue skips the rest of the current iteration and proceeds to the next one.
Example:
Common mistakes and tips Recap
  • Use for loops when you know the number of iterations or are iterating over a collection.
  • Use while loops when the repetition depends on external state or user input.
  • Loops can update variables, perform calculations, and use break/continue for finer control.
  • Combine loops with data structures (lists, dictionaries, files) to build powerful automation patterns.
Next steps You can store multiple items in lists and use loops with those lists to supercharge dashboards, goal trackers, and automated scripts — we’ll cover lists and more advanced iteration techniques next. Links and references

Watch Video

Practice Lab