Ram

Lesson 5 of 19

Loops

Repeat work with for and while loops.

Introduction

Loops are useful when the same operation needs to run repeatedly.

Use a for loop when iterating over a collection or known range, and a while loop when repetition depends on a condition.

Key insight

Repeat work with for and while loops.

Example

for number in range(1, 6):
    print(number)

This example shows the core loops idea in a small, runnable program.

Real-world example

Loops appears in everyday application code, scripts and APIs.

Test yourself

2-question check

1

What is the main idea in Loops?

2

Where can loops be useful?

Ready to check your understanding?

Answer every question, then submit your quiz.

Try it yourself

Python compiler

Runs in your browser. Nothing is sent to a server.

Output appears here

First run downloads the Python runtime, so it may take a moment.

Practice challenge

Print the even numbers from 2 through 10.

Previous
Next