Skip to main content

What are loops?

Loops let you repeat code without writing it multiple times. Instead of copying and pasting, you tell Python to repeat the code for you. Without loops:
With loops:
Both do the same thing, but the loop is much cleaner!

For loops

The for loop is the most common loop in Python. Let’s start simple:

Repeat a specific number of times

Python starts counting at 0, not 1. This is called “zero-indexing”. So range(5) gives you 0, 1, 2, 3, 4 (five numbers total).

Count from different starting points

Loop through text

You can loop through each character in a string:

Loop through a list (preview)

We’ll learn more about lists later, but here’s a preview:

While loops

A while loop continues as long as a condition is true:
Always make sure your while loop will eventually stop! If you forget to update the variable, it will run forever.

Loop Control: break, continue, and else

Python provides keywords to change the normal flow of a loop:

1. break

The break statement exits the loop immediately, skipping any remaining iterations.

2. continue

The continue statement skips the rest of the current iteration and jumps directly to the next iteration.

3. Loop else

A for or while loop can have an else block. The else block runs only if the loop completed normally without encountering a break.

Nested Loops & Breaking Outer Loops

A nested loop is a loop inside another loop.

Breaking from Outer Loops

A break statement only exits the innermost loop containing it. To break out of the outer loop from inside the inner loop, you can use a flag variable:

Common mistakes

Generating Star Patterns with Nested Loops

A structured and general way to print shapes is using nested loops based on two simple building blocks: Increasing Triangles and Decreasing Triangles.
  • The outer loop represents the rows (i).
  • The inner loop(s) represent columns (j).
  • We use print(..., end="") to print characters side-by-side, and a final empty print() to start a new row.
Let’s look at the exact range logic for each shape:

1. Increasing Triangle (Left-Aligned)

To print an increasing triangle, the outer loop i goes from 1 to n, and the inner loop j goes from 1 to i (representing an increasing number of stars per row):

2. Decreasing Triangle (Inverted Left-Aligned)

To print a decreasing triangle, the outer loop i goes from 1 to n, and the inner loop j counts down from n to i with a negative step of -1 (using range(n, i - 1, -1)):

3. Right-Aligned Triangle

A right-aligned triangle is simply a combination of:
  1. A decreasing triangle of spaces (j from i to n - 1).
  2. An increasing triangle of stars (k from 1 to i).

Practice & Exercises

To reinforce what you’ve learned in this section (For Loops, While Loops, Range, and Conditions), practice with these interactive notebooks:

Follow-Along Practice

Practice range configurations, looping over characters, lists, and while loop iterations.💻 VS Code | 🚀 Colab | 📥 Download

Practice Exercises

Test your looping capabilities with challenges on summing series, while loop countdowns, and vowel filtering.💻 VS Code | 🚀 Colab | 📥 Download

What’s next?

Data structures

Learn about lists, dictionaries, and more