Nearby lessons

44 of 159

Python - Nested Loops

📌 What You Will Learn
  • Define nested loops and how one loop sits inside another
  • Explain that the inner loop runs fully for every iteration of the outer loop
  • Write nested for loops to print patterns and matrices
  • Create a multiplication table using nested loops
  • Compare a single loop with a nested loop

What Are Nested Loops?

Nested loops mean placing one loop inside another loop.

The inner loop runs fully for each outer loop iteration.

Why Use Them?

Nested loops are useful for pattern printing, matrix processing, and table generation.

Syntax

Loading editor…

Example 1 - Nested Loop

Loading editor…

Understanding Nested Loop

For every value of i, the inner loop runs completely.

After the inner loop finishes, the outer loop moves to the next value.

Example 2 - Print Rectangle Pattern

Loading editor…

Example 3 - Multiplication Table

Loading editor…

Difference Between Single Loop and Nested Loop

Single Loop Nested Loop
Contains only one loop. Contains one loop inside another loop.
Less repetition. More repetition.
Simple iteration. Useful for tables, matrices, and patterns.
📝 Key Takeaways
  • Nested loops place one loop inside another loop
  • The inner loop runs completely for each iteration of the outer loop
  • Nested loops are used for pattern printing, matrices and table generation
  • A multiplication table prints the product of every row and column pair
  • A single loop does simple iteration while a nested loop handles tables and patterns

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.