Course Content
Python Loops Tutorial
Python Loops Tutorial
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:
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('')
How does the code work?
Task
Print the string matrix using a while
loop nested within a for
loop.
- Configure the outer
for
loop to iterate through the number of rows in the matrix, using the variablei
. - Initialize the variable
j
. - Configure the inner
while
loop to iterate through the number of elements in each row of the matrix, using the variablej
. - Display each element within the nested loop.
- Increment the variable
j
.
Thanks for your feedback!
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:
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('')
How does the code work?
Task
Print the string matrix using a while
loop nested within a for
loop.
- Configure the outer
for
loop to iterate through the number of rows in the matrix, using the variablei
. - Initialize the variable
j
. - Configure the inner
while
loop to iterate through the number of elements in each row of the matrix, using the variablej
. - Display each element within the nested loop.
- Increment the variable
j
.
Thanks for your feedback!
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:
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('')
How does the code work?
Task
Print the string matrix using a while
loop nested within a for
loop.
- Configure the outer
for
loop to iterate through the number of rows in the matrix, using the variablei
. - Initialize the variable
j
. - Configure the inner
while
loop to iterate through the number of elements in each row of the matrix, using the variablej
. - Display each element within the nested loop.
- Increment the variable
j
.
Thanks for your feedback!
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:
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('')
How does the code work?
Task
Print the string matrix using a while
loop nested within a for
loop.
- Configure the outer
for
loop to iterate through the number of rows in the matrix, using the variablei
. - Initialize the variable
j
. - Configure the inner
while
loop to iterate through the number of elements in each row of the matrix, using the variablej
. - Display each element within the nested loop.
- Increment the variable
j
.