๐Ÿ
๐Ÿ Python Fundamentals (Hands-On) ยท PY-101 ยท Session 3 of 6

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.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

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.

๐Ÿ“Œ Worked example
A hundred lines, or three. A beginner wants to print the numbers 1 to 100. Without loops, that's a hundred print statements - tedious and absurd. With a loop it's three lines: `for i in range(1, 101):` then `print(i)`. The same idea scales infinitely: process every user, check every item, draw every side of a shape. The moment loops click, you stop doing repetitive work by hand and start telling the computer to do it - which is a huge part of what programming is for.

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(n) counts from 0 up to n-1. 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!')
โš ๏ธ
Make sure a while loop can end. A 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.

Session 3 of 6 complete

Next up: Functions: Reusable Building Blocks

Continue to Session 4 โ†’

Related lessons

More in this course

Course home → · Browse all courses →