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

Conteúdo do Curso

Python Loops Tutorial

Python Loops Tutorial

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

Nested 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?

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 3. Capítulo 2
toggle bottom row

Nested 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?

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

Seção 3. Capítulo 2
toggle bottom row

Nested 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?

Tarefa

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.

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo

Tudo estava claro?

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?

Tarefa

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.

Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 3. Capítulo 2
Mude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
We're sorry to hear that something went wrong. What happened?
some-alt