Loops (for, while) and iteration patterns
8 minutes read
Beginner friendly
Includes practice
Welcome! Today you’ll learn how Python makes a computer repeat tasks for you.
By the end, you’ll be able to:
• Write a for loop to run through a list or a range of numbers.
• Write a while loop that keeps going until a condition is no longer true.
• Pick the loop style that fits a problem.
Let’s dive in.
The Need for Repetition
Imagine printing every number from 1 to 100 by hand:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
A loop lets you describe “what to repeat” once, then Python does the dull work.
Runtic's Learning Tips
Struggling at first is normal — loops feel odd until you try them a few times.
Focus on the loop pattern (setup → repeat → stop) rather than memorizing syntax.
Keep code short; if you write the same line twice, ask, “Can a loop do this?”
The for Loop Basics
A for loop walks through each item in a sequence.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Behind the scenes:
- Python grabs the first item “Oslo”, puts it in city, runs the body.
- Repeats for every remaining item.
- Stops automatically when the list ends.
Want plain numbers? Use range():
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Quick Practice
2 minutesCheck your 'for'-loop knowledge
Which line is a valid Python for-loop header?
Which call produces the numbers 0,1,2?
The while Loop Basics
A while loop repeats as long as a condition stays True.
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Practice Challenge
Use a while loop that continues running as long as count is greater than 0. Each time the loop runs, subtract 1 from count and add 1 to steps. After the loop finishes, steps should equal 5. Print both count and steps to verify.
Use a while loop to run until count becomes 0, counting how many times it executes.
If you forget to change the variable, the condition never turns False — resulting in an infinite loop.
Common Iteration Patterns
• Index + item together:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
• Early exit with break:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
• Skip current step with continue:
Interactive Code EditorExample Code
pythonThis won’t affect your saved progress.
Quick Summary
Use for with sequences or range when you know how many times to repeat
Use while when repetition depends on a changing condition
break stops a loop early; continue skips the rest of the current turn
Avoiding Infinite Loops
Checklist:
• Make sure something inside the loop changes the condition.
• Test with small numbers first.
• If the program “hangs”, press STOP — then inspect the condition variable.
Practice: Pick the Right Loop
Quick Practice
1 minutesWhich loop style is better?
You must print the first 10 multiples of 3. Which loop is clearer?
Keep Growing!
Even pros sometimes create accidental infinite loops or pick the slower pattern first — the skill is noticing it and fixing quickly.
- You can express repetition in Python with for (known sequence) or while (condition-controlled)
- range(), enumerate(), break, and continue are everyday tools for flexible loops
- Careful condition updates prevent infinite loops and make code safe
- Practice solidifies patterns — tweak, test, and watch your confidence rise!
AI Tutor
Solve first — tutor's here to guide.
Runtric might be wrong. Think critically.