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:For loops
Thefor 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
Awhile loop continues as long as a condition is true:
Loop Control: break, continue, and else
Python provides keywords to change the normal flow of a loop:1. break
Thebreak statement exits the loop immediately, skipping any remaining iterations.
2. continue
Thecontinue statement skips the rest of the current iteration and jumps directly to the next iteration.
3. Loop else
Afor 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
Abreak 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
Forgetting the colon
Forgetting the colon
Wrong indentation
Wrong indentation
Off-by-one errors
Off-by-one errors
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 emptyprint()to start a new row.
1. Increasing Triangle (Left-Aligned)
To print an increasing triangle, the outer loopi 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 loopi 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:- A decreasing triangle of spaces (
jfromiton - 1). - An increasing triangle of stars (
kfrom1toi).
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