Nesting For Loops
Welcome to the Nesting For Loops Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.
Already have an account? Sign In »

Introduction
Welcome to the Nesting For Loops Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.
Learning Outcomes
In this module, you will complete the following exercise:
- Exercise 1 - Using Nested For Loops
After completing this lab, you will be able to:
- Use nested for loops in Python programs
Exam Objectives
The following exam objectives are covered in this lab:
- 2.2 Construct and analyze code segments that perform iteration
Lab Duration
It will take approximately 10 minutes to complete this lab.
Exercise 1 - Using Nested For Loops
Using loops in computer programming allows us to automate and repeat similar tasks multiple times. Recall that a for loop enables repeated execution of code based on a counter and, are, therefore, used most often when the number of iterations is known before entering the loop.
Python programming language allows you to use one for loop inside another for loop, called nested for loops.
The syntax of a nested for loop is as follows:
for [first counter] in [outer loop]: # Outer loop
[body of the outer loop] # Optional
for [second counter] in [nested loop]: # Nested loop
[body of the nested loop]
The nested for loop works as follows:
- The program starts with the outer loop, executing its first iteration.
- This first iteration triggers the inner, nested loop, which is run as many times as the value of the second counter.
- Then the program starts its second iteration of the outer loop thereby triggering the nested loop again.
- After the nested loop runs to completion, the program returns back to the top of the outer loop again
- This process continues until the outer loop runs to completion or a break or other statement disrupts the process. Unless the process is disrupted, the nested for loop runs for first counter x second counter number of times.
In this exercise, you will learn to write a program to display the multiplication tables from 1 to 10 using nested for loop.
Learning Outcomes
After completing this exercise, you will be able to:
- Use nested for loops in Python programs
See the full benefits of our immersive learning experience with interactive courses and guided career paths.