Examples on Nested Loops Python
In this class, we do practice examples on nested loops in python.
For Complete YouTube Video: Click Here
Examples of Nested Loops
Before going into practice, First, we should have basics on nested loop. Click here.
Q1) Display numbers from 0 to 999. Display in 3 digit format with a tab space between each digit.
0 is displayed as 0 0 0.
1 is displayed as 0 0 1.
This example will help you to get a good understanding of nested loops.
From the above program with i =0, The loop with the j variable executes ten times.
For j =0, the loop with the k variable executes ten times.
For j = 1, the loop with the k variable executes ten times. So on.
The above program execution continues.
The output displayed is shown below.
The output continue up to 9 9 9.
Q2) same as Q1. Stop displaying numbers divisible by 5.
From the above program, the logic is similar to Q1.
To check number is divisible by 5. the condition is written in the if statement.
If k equal to 5 or k equal to 0. The number is divisible by 5.
With this logic, we skip that loop iteration.
Continue statement is used to skip a loop iteration.
The output displayed by the above program is shown below.
The output continue up to 9 9 9.