The first program I wrote that felt useful was a tiny script that greeted one person. I typed my own name in, ran it, and grinned at the laptop like an idiot. Then my brother walked in and I wanted to show off, so I copied the line and changed the name. Then my cousin came in. I copied it again. By the time my mum called from the kitchen, I'd realised I had a problem — three nearly-identical lines for three people. What about thirty? Three hundred?
That wall is where most beginners hit a quiet moment of "wait, this can't be the way." It isn't. The way out is three ideas working together: lists, loops, and functions. Once you have them in your hands, your code stops being a single thread and starts handling real work.
Why the three of them, together
A program that processes one thing is fine. A program that processes many — that's where the real work of software lives. Spreadsheets have many rows, class rosters have many students, inboxes have many emails. You will almost never want to handle just one of anything for long.
- Lists are how you hold the many.
- Loops are how you walk through the many.
- Functions are how you stop writing the same instructions over and over.
Take any one away and the others get clumsy. Lists with no loops are just shelves you stare at. Loops with no functions push you back into copy-paste inside the loop body. Functions with no lists only ever get called once. Together, they let you say "for every student on this list, calculate their fees" in three short lines.
Lists — a first look
A list is a row of things, the same way a grocery list is a row of things. You write them down, in order, separated by commas, inside square brackets:
students = ["Amara", "Diego", "Sun-hee", "Kweku"]
print(len(students)) # 4Four names, in the order I typed them. Python will remember that order, and len() will tell me how many names are on the list. There's a lot more to say about lists, and I'll say it next week. For today, hold this in your head: a list is a thing that holds other things, in order. That alone is enough to unlock the next idea.
For loops — wash each plate in the sink
Picture a sink full of plates after dinner. You don't wash them all at once. You pick up one plate, wash it, set it on the rack. Then the next. You keep going until the sink is empty. That's exactly what a for loop does — it picks up one item from your list, runs your instructions on it, then moves on to the next, in order, until there are no items left.
students = ["Amara", "Diego", "Sun-hee", "Kweku"]
for name in students:
print("Welcome,", name)Read it out loud the way I would in class: "For each name in students, print 'Welcome,' followed by the name." The indented line runs once per student, and name gets rebound each time — first to "Amara", then "Diego", then "Sun-hee", then "Kweku".
One thing worth naming. The word name in for name in students: is a choice you make. You could write for student in students: or for x in students: and it would be the same loop. Pick a word that reads well out loud — future-you will thank you.
While loops — keep going while something is true
A while loop has a different shape. Instead of "do this for every item," it says "keep doing this as long as some condition is true." You reach for it when you don't know in advance how many times the loop needs to run — like asking a user for input until they decide to stop:
answer = ""
while answer != "q":
answer = input("Type something (or 'q' to quit): ")
print("You typed:", answer)The loop keeps running as long as answer is not q. The moment the user types q, the condition becomes false and the loop ends.
One honest word of caution. If you forget to change the thing your condition is checking, the loop never ends — that's an infinite loop. You'll write one this term. Everyone does. When you do, hit the stop button on your notebook and look at what's supposed to change inside the loop. The fix is almost always one missing line.
Functions — a recipe you write once and cook many times
Think about a jollof recipe. You write the steps down once — wash the rice, fry the tomato paste, simmer with stock — and you give it a name: jollof. From then on, whenever someone says "let's make jollof tonight," you don't invent the steps again. You follow the recipe. The steps don't change, the amounts do.
A function works the same way. You write a chunk of code once, give it a name, and call that name whenever you want the steps to run. The thing inside the parentheses — the argument — is the ingredient that changes each time.
Here's a tiny one:
def greet(name):
print("Welcome,", name + "!")
greet("Amara")
greet("Diego")Two calls, two greetings, one recipe. To greet thirty people, I don't write thirty print statements — I write the recipe once and call it thirty times. Which, if you look back at the loop above, is exactly what a for loop over a list of names would do.
Putting them together
The three ideas, in five lines:
def greet(name):
print("Welcome,", name + "!")
students = ["Amara", "Diego", "Sun-hee", "Kweku"]
for name in students:
greet(name)That's the shape of a huge amount of real software. A list holds the data. A loop walks through it. A function handles the work for each item. Swap "greet" for "calculate the fee" or "check if they qualify for a scholarship," and you've got the spine of Mini-Project 1.
Take this with you
Don't try to memorise the syntax tonight. Type the small examples above into a notebook, change the names, run them, break them on purpose, fix them. These three ideas click in your hands faster than they do in your head. The first time you write a list, loop through it, and call a function you wrote yourself, pause for a second. That's the moment your code stopped being a single thread. From here on, you're handling real work.