Repeat an action
A for loop repeats an instruction for each item in a sequence. A list stores an ordered set of values such as task names. Use a loop variable that makes the current item clear.
Avoid changing the wrong thing
The body of a loop is controlled by indentation. Check whether a calculation should happen once after the loop or once for every item. Test with an empty list and a list containing one item to find common edge cases.
Try it
Create a list of three course topics and print each one with a loop. Then add an item and verify it appears without writing another print statement.
Key takeaways
- Lists keep ordered values.
- Loops apply work to each item.
- Test empty and one-item cases.