Python · live now

Loops

Repeat work over a collection or until a condition fails: for and while loops, break and continue, and loops nested inside one another.

No account needed to read a lesson · 30-day money-back guarantee

See the full learning path

5

lessons

41

practice questions

27

prerequisite lessons

See it taught

1 of the 5 lessons here is free to preview - the whole explanation and worked example, with no account and no email.

The editor you get

A still of the editor, not a live one

Python Editor

1 2 3 4
total = 0 for n in range(1, 5): total += n print(total)

Editor Output

1

Output:

10

You write and run Python in the browser - nothing to install, no setup. Questions are marked on the values your code produces. The snippet here is an example of the kind of code you write.

The 5 lessons

The 5 lessons in this unit. Expand any one to see what it covers. Each unlocks when its own prerequisites are passed.

01

for loops

Preview

16 questions

Understand how Python's for loop iterates over elements in sequences such as lists, tuples and strings, and how the range() function can control loop repetition for a specified number of iterations.

Covers

For loop overview

range() function

For loops using range()

Iterating over lists, tuples and strings

Preview this lesson - no account needed

02

while loops

4 questions

Understand how to use Python while loops to repeatedly execute code until a specified condition is met, and recognise the importance of updating loop variables to avoid infinite loops.

Covers

While loops

03

Loop control flow

13 questions

Learn how to control loop execution in Python using the continue statement to skip iterations, the break statement to exit early, and the else clause to run code only if the loop completes without a break.

Covers

The continue statement

The break statement

for-else and while-else

04

Nested for loops

4 questions

Learn how a nested for loop executes by having the inner loop complete all its iterations for each run of the outer loop, and use this structure to process complex data like lists of lists.

Covers

Nested for loops

05

Activity: space expedition planner

4 questions

Write Python code to organise a space expedition by generating a task list, tracking supplies, assigning crew tasks, and summarising mission activities.

Covers

Generate task list

Track supply levels

Assign tasks to crew members

Mission activity summary

Where this unit sits in the curriculum

This unit is part of our Python Done Right learning path, which contains 100 lessons. Every one of them is drawn below.

The diagnostic test lets you skip any lesson you already know, including the ones in this unit.

Every dot is a lesson and every line a prerequisite. The 5 red dots are the lessons in Loops; the 27 blue dots feeding into them are the lessons Loops depends on; the 68 pale dots are the rest of the learning path, which come after Loops or alongside it.LoopsFirst lessons on the left100 lessons

Hover any dot to name its lesson.

5 lessons in this unit

27 prerequisite lessons across the unit, counting every step back to the start

68 other lessons in the learning path - after this unit, or alongside it

Every dot is a lesson, every line a prerequisite. You can only start a lesson once you have mastered all of its prerequisites, so you are always building on solid foundations.

Start Loops

One subscription covers every learning path, and you can test out of anything you already know.

One subscription covers every learning path · 30-day money-back guarantee

More in Iteration: Sequences, Comprehensions