Understand Nested Loops with Examples

Nested loops in Python: Explained with examples of patterns.

Rahul Kotecha
Python in Plain English

--

Photo by Joshua Woroniecki on Unsplash

What is a Nested loop?

In the Python programming language, we know that there are mainly two types of loops: for loop and while loop. When we create a for loop and inside the for loop when we create another for or while loop and vice-versa we call it a nested loop. Nested loops are very important when a programmer is dealing with complex problems. We can look at some of the examples of nested loops given below:

Example 1
Example 2
Example 3

Create Certain Patterns Using Nested Loops

Patterns are nothing but a way to express a symbol or something. We will try to create some triangles using a nested loop.

All patterns can be broken down to i and j for better understanding
Pattern 1
Pattern 2
Pattern 3
Pattern 4
Pattern 5
Pattern 6
Pattern 7
Pattern 8
Pattern 9
Pattern 10
Pattern 11
Pattern 12
Pattern 13
Pattern 14
Pattern 15
Pattern 16
Pattern 17
Pattern 18
Pattern 19
Pattern 20
Pattern 21
Pattern 22
Pattern 23
Pattern 24
Pattern 25
Pattern 26
Pattern 27
Pattern 28
Pattern 29
Pattern 30
Pattern 31
Pattern 32
Pattern 33
Pattern 34

There are thousands of patterns that we can create using loops but the understanding of these 34 patterns is what makes the difference. These patterns look easy but if one can create them, they surely have a good understanding of nested loops.

Thank you for reading.

More content at plainenglish.io. Sign up for our free weekly newsletter. Get exclusive access to writing opportunities and advice in our community Discord.

--

--

Student at Stevens Institute of Technology- Masters of Science in Information Systems with a concentration in Business Intelligence and Analytics