Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Nested for Loop | Nested Loops
Python Loops Tutorial
course content

Course Content

Python Loops Tutorial

Python Loops Tutorial

1. The for Loop
2. The while Loop
3. Nested Loops

bookNested 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.

Note

  • 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:

12345678910
matrix = [ [1, 2, 4, 29], [3, 4, 6, 1] ] # Printing every element in the matrix # Outer loop to work with the number of rows for i in range(len(matrix)): # Inner loop to work with the number of element in the row for j in range(len(matrix[i])): print(matrix[i][j], end = ' ') print('')
copy

How does this code work?

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookNested 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.

Note

  • 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:

12345678910
matrix = [ [1, 2, 4, 29], [3, 4, 6, 1] ] # Printing every element in the matrix # Outer loop to work with the number of rows for i in range(len(matrix)): # Inner loop to work with the number of element in the row for j in range(len(matrix[i])): print(matrix[i][j], end = ' ') print('')
copy

How does this code work?

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 2
toggle bottom row

bookNested 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.

Note

  • 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:

12345678910
matrix = [ [1, 2, 4, 29], [3, 4, 6, 1] ] # Printing every element in the matrix # Outer loop to work with the number of rows for i in range(len(matrix)): # Inner loop to work with the number of element in the row for j in range(len(matrix[i])): print(matrix[i][j], end = ' ') print('')
copy

How does this code work?

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

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.

Note

  • 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:

12345678910
matrix = [ [1, 2, 4, 29], [3, 4, 6, 1] ] # Printing every element in the matrix # Outer loop to work with the number of rows for i in range(len(matrix)): # Inner loop to work with the number of element in the row for j in range(len(matrix[i])): print(matrix[i][j], end = ' ') print('')
copy

How does this code work?

Task

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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 3. Chapter 2
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt