Programing (Operations Using Nested Loop) ... Syed Fasih
Programing (Operations Using Nested Loop) ... Syed Fasih
Marks Obtained
Introduction:
One loop inside another loop is called a Nested Loop. In Python, different nested loops can be
formed using for and while loops - a for-in loop inside another for-in loop, a while loop inside
another while loop, a for-in loop inside a while loop, a while inside the for-in loop.
Nested for loops are used when the number of iterations to execute a block of statements
are known beforehand.
# program to print the first three multiplication tables for i in range(1, 4):
print("i =", i, "->", end = " ")
for j in range(1, 11):
print("{:2d}".format(i * j), end = " ")
print() # similar to new line printing after each multiplication tableOutput:
i = 1 -> 1 2 3 4 5 6 7 8 9 10
i = 2 -> 2 4 6 8 10 12 14 16 18 20
i = 3 -> 3 6 9 12 15 18 21 24 27 30
Nested while loops are used when the number of iterations to execute a block of
statements are not known beforehand.
Output:
1 2 31 2 31 2 3
Other Nested Loops (for inside while and while inside for)
Python allows us to write a for loop inside a while loop and a while inside a for loop as shown
below.
Here, the while loop executes until the length of the given sequence. For each iteration of
the while loop, for loop prints all the elements of the given sequence.
Output:
40
FACEPrep
40
FACEPrep
Since the given list contains 2 elements, the for loop executed two times and we got the
output twice. while loop executes until the length of the given sequence.
In Python, break is used to terminate the current iteration and proceed with the next iteration. If a
break statement is used inside an inner loop, then the current iteration of the inner loop gets
stopped. If it is used inside an outer loop, then the current iteration of the outer loop gets stopped.
Output:
40
FACEPrep
40
FACEPrep
40
FACEPrep
40
FACEPrep
Here, the while loop executes four times. Hence we got the output four times. Also for
loop breaks when if condition becomes True.
Here, the for loop skips the element zero and prints the remaining elements of the
sequence until the length of the sequence.
One loop inside another loop is called a nested loop. These nested loops are mainly used to
traverse through multi-dimensional arrays, matrix values, tabular data.
We can break nested loops in Python using the control statement ‘break’. Whenever an
interpreter comes across the word ‘break’, it stops the current iteration and proceeds with the
next iteration.
Yes, we can use a while loop inside for loop and for loop inside a while loop.
TASK 1
Print the fallowing output using nested for
loop
22
333
4444
Code Explanation:
Initializing a variable called “depth” and give it a value of 5 with the help of command:
depth = 5
Going ahead, we have Nested for loop. We have the outer for loop to set the depth or
number of rows in this pattern. When we use the command:
This helps us to get a list of numbers 0-4. Because the depth value is 5, this means that 5
is exclusive and that is why the range goes from 0-4.
Then, we set the inner for loop using this command:
for i in range(number):
In the inner for loop, initially, the value of the number is 0, that’s why we skip the first
row. Then we go back to the outer for loop and the number value becomes 1. Once the
number value is 1, we head inside the inner loop and print 1.
Output:
This program gives us specific pattern of required numbers
TASK 2
Print the * pattern using nested for loops
*
**
***
****
*****
****
***
**
*
Code Explanation:
So you can run 2 different loops one which creates the upper triangle and another which
creates the lower triangle.
Reference:
https://www.tutorialstonight.com/
Output:
python/pattern-program-in-
python
This program gives us right Pascal triangle (* pattern)
TASK 3
Print your name in 3 rows & columns
Code Explanation:
for x in range(3):
This helps us to obtain 3 rows
In the inner for loop, we have set the value of required columns we want.
for x in range(3):
Using athematic operator (+) with “a” and stopping command by using end=”” &
breaking command and to exert in second line i.e. print ( ) we run our program.
Output:
This program provided my name and can provide any name in specific order as per user demand
Code Explanation:
In this program, the outer for loop is iterate
numbers from 1 to 10. The range() return 10
numbers. So total number of iteration of the outer
loop is 10.
In the first iteration of the nested loop, the number
is 1. In the next, it 2 & so on till 10.
Next, For each iteration of the outer loop, the
inner loop will execute ten times. The inner loop
will also execute ten times because we are printing multiplication table up to ten.
In each iteration of an inner loop, we calculated the multiplication of two numbers.
Output:
Multiplication tables up to 10 are generated using nested loop.
54321
4321
321
21
1
Code Explanation:
for i in range(0,rows+1)):
for j in range(rows-i,0,-1):
Printing j and stopping command by using end=”” & breaking command and to exert
in second line i.e. print ( ) we run our program.
Output:
This program provide reverse number pattern of given task.
A nested loop is a loop inside a loop. Therefore a nested for loop is a for loop inside
another for loop.
The inner for loop is executed for each execution of the outer for loop. If the outer for
loop has range(a) and the inner for loop has range(b), then the inner for loop executes a*b
times.
Nested for loops can be used to solve problems such as prime numbers in a given range,
pattern printing, multiplication table printing, etc.