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

Contenido del Curso

Python Loops Tutorial

Python Loops Tutorial

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

While Loop inside a for Loop

Using one type of loop within another is a commonly employed technique.

Today, we'll explore utilizing a while loop within a for loop. You can accomplish the same task by employing a for loop within a while loop.

Examine the code below:

1234567891011
names = ['Anna', 'Richard', 'John', 'Patrick'] # Printing every name for 3 times # Setting outer for loop to work with names for i in names: j = 0 # Setting inner while loop to work with the number of name's printing while j < 3: print(i, end = ' ') j += 1 print('')
copy

How does the code work?

Tarea

Print the string matrix using a while loop nested within a for loop.

  1. Configure the outer for loop to iterate through the number of rows in the matrix, using the variable i.
  2. Initialize the variable j.
  3. Configure the inner while loop to iterate through the number of elements in each row of the matrix, using the variable j.
  4. Display each element within the nested loop.
  5. Increment the variable j.

Tarea

Print the string matrix using a while loop nested within a for loop.

  1. Configure the outer for loop to iterate through the number of rows in the matrix, using the variable i.
  2. Initialize the variable j.
  3. Configure the inner while loop to iterate through the number of elements in each row of the matrix, using the variable j.
  4. Display each element within the nested loop.
  5. Increment the variable j.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 3. Capítulo 4
toggle bottom row

While Loop inside a for Loop

Using one type of loop within another is a commonly employed technique.

Today, we'll explore utilizing a while loop within a for loop. You can accomplish the same task by employing a for loop within a while loop.

Examine the code below:

1234567891011
names = ['Anna', 'Richard', 'John', 'Patrick'] # Printing every name for 3 times # Setting outer for loop to work with names for i in names: j = 0 # Setting inner while loop to work with the number of name's printing while j < 3: print(i, end = ' ') j += 1 print('')
copy

How does the code work?

Tarea

Print the string matrix using a while loop nested within a for loop.

  1. Configure the outer for loop to iterate through the number of rows in the matrix, using the variable i.
  2. Initialize the variable j.
  3. Configure the inner while loop to iterate through the number of elements in each row of the matrix, using the variable j.
  4. Display each element within the nested loop.
  5. Increment the variable j.

Tarea

Print the string matrix using a while loop nested within a for loop.

  1. Configure the outer for loop to iterate through the number of rows in the matrix, using the variable i.
  2. Initialize the variable j.
  3. Configure the inner while loop to iterate through the number of elements in each row of the matrix, using the variable j.
  4. Display each element within the nested loop.
  5. Increment the variable j.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Sección 3. Capítulo 4
toggle bottom row

While Loop inside a for Loop

Using one type of loop within another is a commonly employed technique.

Today, we'll explore utilizing a while loop within a for loop. You can accomplish the same task by employing a for loop within a while loop.

Examine the code below:

1234567891011
names = ['Anna', 'Richard', 'John', 'Patrick'] # Printing every name for 3 times # Setting outer for loop to work with names for i in names: j = 0 # Setting inner while loop to work with the number of name's printing while j < 3: print(i, end = ' ') j += 1 print('')
copy

How does the code work?

Tarea

Print the string matrix using a while loop nested within a for loop.

  1. Configure the outer for loop to iterate through the number of rows in the matrix, using the variable i.
  2. Initialize the variable j.
  3. Configure the inner while loop to iterate through the number of elements in each row of the matrix, using the variable j.
  4. Display each element within the nested loop.
  5. Increment the variable j.

Tarea

Print the string matrix using a while loop nested within a for loop.

  1. Configure the outer for loop to iterate through the number of rows in the matrix, using the variable i.
  2. Initialize the variable j.
  3. Configure the inner while loop to iterate through the number of elements in each row of the matrix, using the variable j.
  4. Display each element within the nested loop.
  5. Increment the variable j.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones

¿Todo estuvo claro?

Using one type of loop within another is a commonly employed technique.

Today, we'll explore utilizing a while loop within a for loop. You can accomplish the same task by employing a for loop within a while loop.

Examine the code below:

1234567891011
names = ['Anna', 'Richard', 'John', 'Patrick'] # Printing every name for 3 times # Setting outer for loop to work with names for i in names: j = 0 # Setting inner while loop to work with the number of name's printing while j < 3: print(i, end = ' ') j += 1 print('')
copy

How does the code work?

Tarea

Print the string matrix using a while loop nested within a for loop.

  1. Configure the outer for loop to iterate through the number of rows in the matrix, using the variable i.
  2. Initialize the variable j.
  3. Configure the inner while loop to iterate through the number of elements in each row of the matrix, using the variable j.
  4. Display each element within the nested loop.
  5. Increment the variable j.

Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
Sección 3. Capítulo 4
Cambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt