runtric-logo
Python Basic 2 - DataLoops (for, while) and iteration patterns (5/5)
Chapter 5
Python Basic 2 - Data

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:

Example Code

Interactive Code Editor
python

This 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.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Behind the scenes:

  1. Python grabs the first item “Oslo”, puts it in city, runs the body.
  2. Repeats for every remaining item.
  3. Stops automatically when the list ends.

Want plain numbers? Use range():

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Quick Practice

2 minutes

Check 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.

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

Practice Challenge

Interactive Code Editor
📝 Your Task:

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.

python

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:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

• Early exit with break:

Example Code

Interactive Code Editor
python

This won’t affect your saved progress.

• Skip current step with continue:

Example Code

Interactive Code Editor
python

This 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 minutes

Which 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.

Lesson Complete!
100%
Progress
What You've Learned:
  • 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!

Sign in to track your progress

Chapter Progress

0 secs

AI Tutor

Powered by Runtric

Solve first — tutor's here to guide.

Runtric might be wrong. Think critically.