Repeating with Loops
Computers are brilliant at doing the same thing many times. Loops let you repeat actions without rewriting them โ over a list, a range of numbers, or until a condition changes.
One of the things computers do best is repetition - doing something many times, fast, without getting bored. Loops are how you tell a program to repeat. Instead of writing the same line a hundred times, you write it once inside a loop. This is where code starts to feel powerful, and where drawing with larzturtle becomes genuinely fun.
for loops: repeat over items
A for loop repeats once for each item in a collection (or each number in a range).
for name in ['Ada', 'Bob', 'Cara']:
print('Hello, ' + name)
for i in range(5): # i = 0, 1, 2, 3, 4
print(i)
# draw a square with a turtle (larzturtle):
for _ in range(4):
t.forward(100)
t.right(90)range(5) gives 0,1,2,3,4 - five numbers, starting at 0 (programmers count from 0). Use a for loop over a list to do something with each item, or over a range to repeat a fixed number of times. The loop variable (name, i) takes each value in turn.while loops: repeat until
A while loop repeats as long as a condition stays True - useful when you don't know the count in advance.
count = 3
while count > 0:
print(count)
count = count - 1 # important: change the condition!
print('Go!')while loop keeps going as long as its condition is True - so something inside must eventually make it False, or it loops forever (an 'infinite loop'). In the example, count = count - 1 is what lets it finish. Forgetting to change the condition is a classic beginner bug (and exactly what the sandbox's timeout protects against).Loop with confidence
- for repeats over items (a list) or a range of numbers.
- range(n) gives 0..n-1 - programmers count from 0.
- while repeats until its condition becomes False - so make sure it can.
- Loops + turtle = fun: a few lines draw complex shapes.
๐ฏ Key takeaways
- Loops repeat actions without rewriting them - computers excel at repetition.
- for loops repeat over items in a collection or numbers in a range; the loop variable takes each value.
- range(n) counts 0 to n-1 (programmers count from 0).
- while loops repeat until their condition is False - always ensure something can make it stop.
Frequently asked
When do I use a for loop versus a while loop?
Use a for loop when you know what you're looping over - a specific list of items, or a fixed number of repetitions via range(). It's the most common loop and it's tidy because the loop variable automatically steps through each value. Use a while loop when you don't know in advance how many times you'll repeat, only that you want to continue until some condition changes - like 'keep asking until the user types quit' or 'keep going while there's work left'. Rule of thumb: known count or known collection -> for; repeat-until-something-happens -> while. Many problems can use either, but this guideline picks the natural fit.
Why does range(5) give 0 to 4 instead of 1 to 5?
Because programming conventionally counts from 0, and range(n) is designed to produce exactly n numbers starting at 0 - so range(5) gives 0, 1, 2, 3, 4, which is five values. This 'zero-based' counting feels odd at first but is deeply ingrained (list positions also start at 0), and it's actually convenient: range(len(mylist)) gives exactly the valid positions of a list. If you want 1 to 5, you can write range(1, 6) (start at 1, stop before 6). You'll quickly get used to counting from 0 - it's one of those small conventions that clicks with a little practice.
Related lessons
Making Decisions with if
Programs get useful when they can decide. if / elif / else let your code do different thinโฆ
Python Fundamentals (Hands-On)Functions: Reusable Building Blocks
Functions let you name a chunk of code and reuse it. They're how you avoid repetition, orgโฆ
Learn to Code (from Zero)How Programs Think
Almost all programs are built from a few simple building blocks - values, decisions and reโฆ