Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

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

`for` loop
`while` loop

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)

Worked 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 value

  • running_total is an accumulator because it stores the growing result

  • the 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?

When you already have a list, string, or range to iterate throughCorrect. A `for` loop fits best when the items to visit are already known.
When you want a loop that never changes stateA good loop needs a clear path toward completion.
Only when every item is a number`for` loops work with many iterable types, not just numbers.

What is the main risk in a poorly designed `while` loop?

It will automatically convert lists into dictionariesLoops do not change data types by themselves.
The condition may never become false, causing an infinite loopCorrect. A `while` loop must update something that eventually stops the repetition.
It can only print one line of outputA loop can run its body many times.
Mini recap
  • loops help you apply the same rule across many values or states

  • for is usually best for known collections

  • while is best when repetition depends on a condition changing over time

  • loops become especially powerful when paired with if checks

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

  • for loops are ideal for collections and ranges

  • while loops depend on a condition that changes over time

  • accumulators and counters are common loop patterns in analytics and automation

The next notebook shows how to package repeated logic into reusable functions.