Repeating Logic Across Customers, Days, and Datasets¶

Loops turn one decision into a repeatable workflow¶
Control flow taught Python how to choose a branch. Loops teach Python how to apply that same logic across every customer, every product, every day, or every row in a dataset without copying the same code again and again.
Why this matters in business¶
Real work is repetitive in the useful sense. Analysts, engineers, and ML practitioners often need the same rule to run many times:
notify every customer with an overdue payment
total sales across many days or stores
scan a stream of events and keep only the risky ones
repeat a process until a stock level, quality score, or forecast threshold is reached
Two common loop patterns¶
Use a for loop when Python already knows what should be visited, such as a list of customers, a string of characters, or a range() of numbers.
for customer in customers:
print(customer)Use a while loop when repetition should continue only while a condition remains true.
stock = 12
while stock < 20:
stock += 2Worked example: weekly sales review¶
The loop below processes one value at a time and builds a running total.
What to notice
enumerate(..., start=1)gives both the position and the valuerunning_totalis an accumulator because it stores the growing resultthe indented block runs once for each item in the list
Worked example: restock until the shelf is full¶
A while loop is useful when you do not know the exact number of repetitions ahead of time, but you do know the stopping rule.
Why this example matters
This pattern shows up in simulations, retry logic, inventory management, and iterative model-tuning workflows. The key design question is always: what variable changes each time so the loop can eventually stop?
Filtering while iterating¶
Loops become more useful when paired with conditions. Here the program prints only the orders that need urgent attention.
Visual intuition: counter-based repetition¶
Common mistakes to avoid¶
customers = ["Asha", "Daniel", "Maria"]
for customer in customers:
print("Sending reminder to", customer)Sending reminder to Asha
Sending reminder to Daniel
Sending reminder to Maria
Guided Practice¶
When is a `for` loop usually the clearest choice?¶
What is the main risk in a poorly designed `while` loop?¶
Mini recap
loops help you apply the same rule across many values or states
foris usually best for known collectionswhileis best when repetition depends on a condition changing over timeloops become especially powerful when paired with
ifchecks
Quick bridge¶
In the next notebook, functions will let you package a useful loop into a reusable block so you can call it whenever the same task appears again.
Practice Lab¶
Exercise 1: Print each overdue customer¶
Hint
Start with for customer in overdue_customers: and indent the print(...) statement underneath it.
Exercise 2: Build a running total¶
Hint
Use a for loop with a variable like count or orders, then update total_orders += ....
Exercise 3: Restock until target¶
Hint
Your condition should compare stock with target. Inside the loop, update both stock and rounds.
Key Takeaways¶
loops repeat logic without repeating source code
forloops are ideal for collections and rangeswhileloops depend on a condition that changes over timeaccumulators and counters are common loop patterns in analytics and automation
The next notebook shows how to package repeated logic into reusable functions.