0Pricing
Learn AI with Python · Lesson

Looping

Understand for and while loops to automate repetitive tasks in your programs.

Looping is a free Learn AI with Python lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

1

Introduction to Loops

Loops allow you to repeat a block of code multiple times, making your programs more efficient and concise.

In this lesson, you’ll learn about different types of loops in Python: for, while, and nested loops.

Looping — illustration 1

2

What is a Loop?

A loop is used to execute a block of code repeatedly until a certain condition is met. For example:

This prints the numbers 0 to 4.

# Example of a loop
for i in range(5):
    print(i)

3

The for Loop

The for loop iterates over a sequence (like a list or range) and executes a block of code for each item:

# Example of a for loop
names = ["Alice", "Bob", "Charlie"]
for name in names:
    print("Hello,", name)

4

The while Loop

The while loop continues to execute a block of code as long as its condition is True:

# Example of a while loop
counter = 0
while counter < 3:
    print(counter)
    counter += 1

5

Breaking Out of a Loop

You can use the break statement to exit a loop prematurely:

This stops the loop when i is equal to 5.

# Using break to exit a loop
for i in range(10):
    if i == 5:
        break
    print(i)

6

Skipping Iterations with continue

The continue statement skips the rest of the current iteration and moves to the next:

This skips printing 2.

# Using continue to skip iterations
for i in range(5):
    if i == 2:
        continue
    print(i)

7

Nested Loops

A loop inside another loop is called a nested loop. For example:

# Example of nested loops
for i in range(3):
    for j in range(2):
        print(f"i = {i}, j = {j}")

8

9

Common Mistakes with Loops

Here are some common mistakes to avoid:

  • Forgetting to update the loop variable in a while loop (causing an infinite loop).
  • Misusing break and continue statements.
  • Incorrect indentation in nested loops.

10

What Did We Learn?

In this lesson, you learned:

  • The basics of for and while loops.
  • How to use break and continue to control loop behavior.
  • How to create and use nested loops.

Great job! Let’s move on to the next topic.

Looping — illustration 10

Frequently asked questions

Is the “Looping” lesson free?

Yes — the full text of “Looping” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Looping”?

Understand for and while loops to automate repetitive tasks in your programs. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Looping” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Understanding Conditionals
  2. Boolean Logic and Comparisons
  3. Looping
  4. Assignment Expressions (Walrus Operator)
← Back to Learn AI with Python