Doing something to every item — a loop
P41.repeating-and-choosing.01 · Audience: Guest, Language professional, IT/ML learner · Prerequisites: Writing your own tiny function
Where next?
Later in Repeating & Choosing
This module unlocks
Go up a level
P41.repeating-and-choosing.01 · Audience: Guest, Language professional, IT/ML learner · Prerequisites: Writing your own tiny function
Later in Repeating & Choosing
This module unlocks
Go up a level
Here is the move at the heart of automation. Picture a column of words and a task you repeat down the whole column: capitalise each one, or note how long each one is. On paper you go line by line, doing the same thing to each. A loop is that "go down the list, do this to each one" — written once, run for every item.
for word in words: reads as "for each word in the list words, do the following." Python hands you the items one at a time — first "pear", then "apple", and so on — and runs the indented lines once for each. You write the task once; it happens to every item.Why it matters — Anything you do to a whole list by hand — every entry, every line — a loop does once and applies to all of them. It is the single idea that makes a notebook worth the trouble.
Ask a question about this content. The mentor explains and grounds its answer in what you are studying; asking is recorded as a learning signal, not a grade.
Off by default. The mentor always gets your file; this only decides whether your own copy stays here. Copies live in this browser only - they do not follow you to another device, and clearing site data removes them.
Your turn: loop over a list of words and build a new list holding a capitalised copy of each — do this to each one. Press Run.
Loading exercise…
Loading your notes...