Understanding Nested loop in python

AAyushporwal
2 min readNov 30, 2023

--

Here we would learn, How to write Nested loop, where to write it and how to use it, which type of pattern we can make from it etc.

Nested Loops:

In simple way, it means loop within a loop. Why it is important. It is important as if you know it deeply, it means you have the ability to build logic clearly that can give us an edge from others. There are many types of questions based on this like pattern building etc.

Nested loop can be of two types-

1- While loop 2- For Loop

In this topic, we will get to know how for loop work.

Example:

for loop

Output:

When we see this type of pattern, it is called Nested loop.

Here, range of i is (1 to 3) and first i will give output as 1, and after this it will go to j where j range is (10 to 13). We need to know that for loop never stop until it get the result.

It means for each value of i, all the values of j will be printed (10 to 13). Now j loop is ended.

So, it is a complete loop of i for a single value.

Output:

1

10

11

12

13

Again it will come to i and print value 2, then again goto loop of j and print all the value of j from (10 to 13)

Output:

2

10

11

12

13

Again it will come to i and print value 3 and goto loop j and print all its values.

Output:

3

10

11

12

13

Now all the values are completed as, the range always work from (starting position, end position -1). Now our loop is completely ended.

--

--

AAyushporwal

An experience content writer with overall experience of 5 years in edtech and IT industry. I write articles based on Python , SQL, ML , DL and Data science.