# Nested Loops in Python: Explained with Examples

In Python programming, loops are used to iterate over a sequence of elements. Nested loops come into action when we want to iterate over a sequence of sequences. In this article, we will learn about nested loops in Python and how they are implemented with examples.

## Syntax of Nested Loops in Python

The syntax of nested loops in Python is as follows:

```
for sequence_1 in sequence_1_list:
for sequence_2 in sequence_2_list:
# perform some action
```

The outer loop iterates over the elements of the outer sequence, while the inner loop iterates over the elements of the inner sequence. The inner loop is executed for each iteration of the outer loop, effectively producing a grid or matrix of elements. The action to be performed can be anything, such as printing the elements, performing some calculation, or updating a data structure, depending on the requirements of the program.

## Examples of Nested Loops in Python

Let’s look at some examples of nested loops in Python to understand their implementation and usage.

### Example 1: Multiplication Table

A common example of nested loops is to print the multiplication table of a given number. Here’s how you can do it using nested loops:

```
num = int(input("Enter a number: "))
# iterating over rows
for i in range(1, 11):
# iterating over columns
for j in range(1, num+1):
print(j, "*", i, "=", j*i, end="\t")
print()
```

In this example, the outer loop iterates over the rows of the table, while the inner loop iterates over the columns of the table. The result of the multiplication is printed in the format of j * i = j*i using the print() function.

### Example 2: Pattern Printing

Nested loops can also be used to print patterns using symbols or characters. Here’s an example:

```
rows = int(input("Enter the number of rows: "))
# iterating over rows
for i in range(rows):
# iterating over columns
for j in range(i+1):
print("*", end="")
print()
```

This program prints a pattern of stars in increasing order of the number of stars in each row. The outer loop iterates over the rows, while the inner loop prints the stars for each row based on the value of i.

### Example 3: Matrix Addition

Nested loops can also be used to perform operations on matrices or multi-dimensional arrays. Here’s an example of matrix addition:

```
# create two matrices
matrix_a = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
matrix_b = [[9, 8, 7],
[6, 5, 4],
[3, 2, 1]]
# initialize the result matrix with zeros
result_matrix = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
# iterate over rows
for i in range(len(matrix_a)):
# iterate over columns
for j in range(len(matrix_a[i])):
# perform element-wise addition
result_matrix[i][j] = matrix_a[i][j] + matrix_b[i][j]
# print the result matrix
print("Matrix A:")
for row in matrix_a:
print(row)
print("Matrix B:")
for row in matrix_b:
print(row)
print("Result Matrix:")
for row in result_matrix:
print(row)
```

In this example, two matrices are created using nested lists. The result matrix is also initialized with zeros using nested lists. The outer loop iterates over the rows, while the inner loop iterates over the columns of each row. The corresponding elements of the matrices are added together and stored in the result matrix. Finally, all three matrices are printed using nested loops.

## Conclusion

Nested loops are an important tool in Python programming when dealing with multi-dimensional data structures. They allow us to iterate over all possible combinations of elements in a nested sequence of sequences. In this article, we learned about the syntax and examples of nested loops in Python.