Nested Loops
Dive deeper into loops within loops
Nested Loops is a free Python For Kids 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 Python For Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Nested Loops
Welcome back to the Control Flow lesson! Today, we'll dive deeper into loops by learning about nested loops, which are loops inside other loops.

2
What are Nested Loops?
Nested loops are loops within loops. They allow you to perform more complex iterations, such as going through multi-dimensional data structures like grids or tables.
- Outer Loop: The first loop that contains the inner loop.
- Inner Loop: The loop inside the outer loop.
3
Why Use Nested Loops?
Nested loops are useful when you need to perform repetitive tasks within repetitive tasks. For example, creating patterns, processing rows and columns in a grid, or handling multi-dimensional data.
Example Use Cases:
- Printing a multiplication table.
- Creating complex patterns with stars or numbers.
- Iterating through a list of lists.
4
Example of Nested For Loops
Let's look at how nested for loops work with a simple example.
Example:
# Outer loop iterates over numbers 1 to 3
for i in range(1, 4):
# Inner loop iterates over letters 'A' to 'C'
for letter in ['A', 'B', 'C']:
print(i, letter) # Prints the current number and letter
5
Output of the Nested For Loop Example
When you run the above code, Python will print each combination of number and letter:
1 A
1 B
1 C
2 A
2 B
2 C
3 A
3 B
3 C6
Example of Nested While Loops
Nested while loops work similarly to nested for loops. Here's how you can use them:
Example:
# Initialize the outer loop counter
i = 1
while i <= 3:
# Initialize the inner loop counter
j = 1
while j <= 2:
print("Outer:", i, "Inner:", j) # Prints the current outer and inner counters
j += 1 # Increment inner counter
i += 1 # Increment outer counter
8
Creating Patterns with Nested Loops
Nested loops are great for creating patterns. Let's create a simple star pattern using nested for loops.
Example:
# Number of rows for the pattern
rows = 5
# Outer loop for each row
for i in range(1, rows + 1):
# Inner loop for printing stars
for j in range(i):
print("*", end=" ") # Prints a star and stays on the same line
print() # Moves to the next line after inner loop completes
9
10
Great Job!
You've learned how to use nested loops to perform complex iterations in your Python programs. Nested loops help you handle multi-dimensional data and create intricate patterns. Keep practicing by creating your own nested loops and exploring different scenarios!

Frequently asked questions
Is the “Nested Loops” lesson free?
Yes — the full text of “Nested Loops” is free to read here on the web, and the Python For Kids 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 Python For Kids course, upgrade to CoddyKit PRO.
What will I learn in “Nested Loops”?
Dive deeper into loops within loops You practise Python For Kids 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 Python For Kids?
No prior experience is required. Python For Kids 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 “Nested Loops” 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 Python For Kids lesson?
Yes. Every Python For Kids 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.