Python for Loops: Process Multiple Records Without Repetition
A for loop in Python processes each item in a sequence without manual repetition. Learn to iterate through a list, unpack tuples, and apply functions to multiple records at once.
OK so yesterday we added the VIP/High Value/Standard tiers to format_sale. The function works perfectly for one sale at a time. But here's the problem — you said Diane's Monday report has 50 sales. Are we really going to call format_sale 50 times?
That's exactly the question that leads to loops. A for loop in Python runs a block of code once for each item in a sequence. Instead of calling format_sale fifty times, you write the loop once and Python handles all 50 automatically. Check this out:
sales = [("Alice", 6800), ("Bob", 340), ("Carol", 1250)]
for sale in sales:
print(sale)
That for sale in sales: means "for each item in the sales list, assign it to the variable sale and run the code below it." Python does the rest.
Wait, so sale is just a variable that holds each item one at a time? Like, first it's Alice and 6800, then Bob and 340?
Exactly. The variable sale gets a new value on each loop iteration. In this case, sale is a tuple — remember those from Day 3? Two values packed together. So on the first iteration, sale is ("Alice", 6800). On the second, it's ("Bob", 340). And so on. Python counts automatically — you don't have to write a counter or track an index.
for sale in sales:
print(sale) # First: ('Alice', 6800)
# Second: ('Bob', 340)
# Third: ('Carol', 1250)
But how do I get the name and amount out of the tuple? I need them separately to call format_sale.
You unpack them. This is beautiful Python syntax:
for sale in sales:
name, amount = sale
print(format_sale(name, amount))
The line name, amount = sale unpacks the tuple. Python sees two variables on the left and two values in the tuple, and it matches them up automatically. No indexing, no sale[0] and sale[1] — just name, amount = sale and you're done.
So Python is smart enough to know that the tuple has two things and the left side has two variables, so it just... distributes them?
That's exactly how unpacking works. It's one of the reasons people love Python — the code reads almost like plain English. name, amount = sale literally means "assign the first item to name and the second to amount."
So if I have the sales list from this week, I can loop through all 5 and print each one formatted with the tiers?
Let's build it. Here's the sales data:
sales = [
("Alice Chen", 6800.00),
("Bob Kumar", 340.50),
("Carol Santos", 1250.00),
("David Park", 89.99),
("Eve Williams", 5000.00),
]
for sale in sales:
name, amount = sale
print(format_sale(name, amount))
Python will unpack each tuple, call format_sale, and print the result. Five sales, five lines of output, one simple loop.
This is it, right? This is what I've been building toward all week. The function that started as just returning a name and amount, then added strip(), then f-strings, then the tier logic — now it's actually processing multiple records.
You've just gone from processing one sale manually to processing 50, 500, or 50,000 with the exact same loop. That's the real power of programming. You've been building the pieces all week — format_sale with strip, f-strings, tier logic. Today you see them working together at scale. This is exactly what Diane's Monday report automation looks like. Your Monday morning work went from an hour of Excel copying and formatting to running this loop and getting all 50 sales formatted, tiered, and ready to send. You've spent five days building a function that formats and classifies sales. Tomorrow we're going to test what stuck — and I think you'll surprise yourself.
Practice your skills
Sign up to write and run code in this lesson.
Python for Loops: Process Multiple Records Without Repetition
A for loop in Python processes each item in a sequence without manual repetition. Learn to iterate through a list, unpack tuples, and apply functions to multiple records at once.
OK so yesterday we added the VIP/High Value/Standard tiers to format_sale. The function works perfectly for one sale at a time. But here's the problem — you said Diane's Monday report has 50 sales. Are we really going to call format_sale 50 times?
That's exactly the question that leads to loops. A for loop in Python runs a block of code once for each item in a sequence. Instead of calling format_sale fifty times, you write the loop once and Python handles all 50 automatically. Check this out:
sales = [("Alice", 6800), ("Bob", 340), ("Carol", 1250)]
for sale in sales:
print(sale)
That for sale in sales: means "for each item in the sales list, assign it to the variable sale and run the code below it." Python does the rest.
Wait, so sale is just a variable that holds each item one at a time? Like, first it's Alice and 6800, then Bob and 340?
Exactly. The variable sale gets a new value on each loop iteration. In this case, sale is a tuple — remember those from Day 3? Two values packed together. So on the first iteration, sale is ("Alice", 6800). On the second, it's ("Bob", 340). And so on. Python counts automatically — you don't have to write a counter or track an index.
for sale in sales:
print(sale) # First: ('Alice', 6800)
# Second: ('Bob', 340)
# Third: ('Carol', 1250)
But how do I get the name and amount out of the tuple? I need them separately to call format_sale.
You unpack them. This is beautiful Python syntax:
for sale in sales:
name, amount = sale
print(format_sale(name, amount))
The line name, amount = sale unpacks the tuple. Python sees two variables on the left and two values in the tuple, and it matches them up automatically. No indexing, no sale[0] and sale[1] — just name, amount = sale and you're done.
So Python is smart enough to know that the tuple has two things and the left side has two variables, so it just... distributes them?
That's exactly how unpacking works. It's one of the reasons people love Python — the code reads almost like plain English. name, amount = sale literally means "assign the first item to name and the second to amount."
So if I have the sales list from this week, I can loop through all 5 and print each one formatted with the tiers?
Let's build it. Here's the sales data:
sales = [
("Alice Chen", 6800.00),
("Bob Kumar", 340.50),
("Carol Santos", 1250.00),
("David Park", 89.99),
("Eve Williams", 5000.00),
]
for sale in sales:
name, amount = sale
print(format_sale(name, amount))
Python will unpack each tuple, call format_sale, and print the result. Five sales, five lines of output, one simple loop.
This is it, right? This is what I've been building toward all week. The function that started as just returning a name and amount, then added strip(), then f-strings, then the tier logic — now it's actually processing multiple records.
You've just gone from processing one sale manually to processing 50, 500, or 50,000 with the exact same loop. That's the real power of programming. You've been building the pieces all week — format_sale with strip, f-strings, tier logic. Today you see them working together at scale. This is exactly what Diane's Monday report automation looks like. Your Monday morning work went from an hour of Excel copying and formatting to running this loop and getting all 50 sales formatted, tiered, and ready to send. You've spent five days building a function that formats and classifies sales. Tomorrow we're going to test what stuck — and I think you'll surprise yourself.