for, range, and enumerate
Scenario: The same health check must run for every service.
New words, in plain English
- Iteration - one trip through a loop
- Iterable - an object that can supply values one by one
- range - a lazy sequence of integers
A for loop asks an iterable for each value. range(start, stop, step) excludes stop. enumerate adds counters; zip pairs multiple iterables.
Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.
A small, real example
services = ["api", "db"]
for number, service in enumerate(services, 1):
print(number, service)
for retry in range(3):
print(retry)
Tip: Loop directly over values unless you genuinely need an index.
Goal: Practice this idea in the py-conditionals-loops challenge at/labs/python. Fix the broken program, run it yourself, then usecheckto prove the real end state.