course content

Course Content

Python Loops Tutorial

Nested for LoopNested for Loop

Let's delve into the world of matrices!

With the aid of a nested loop, we can manipulate matrices.

In mathematics, a matrix is a rectangular array or table of numbers, symbols, or expressions organized in rows and columns. It's used to represent a mathematical object or a characteristic of such an object.

In Python, a matrix is a data structure composed of nested lists.

The outer loop will handle the matrix's rows, while the inner loop will manage the columns, or vice versa.


  • len(matrix) signifies the number of rows.
  • len(matrix[i]) represents the count of elements in a row (equivalent to the number of columns). Or vice versa.

Examine the code below:

How does this code work?


You must calculate the sum of all elements within the matrix.

  1. Initialize counter = 0.
  2. Configure the outer for loop to iterate through the number of rows in the matrix.
  3. Configure the inner for loop to iterate through the number of elements in each row of the matrix.
  4. Accumulate the sum using the counter variable.
  5. Display the value stored in the counter.

Everything was clear?

Section 3. Chapter 2
toggle bottom row