0% found this document useful (0 votes)
33 views7 pages

BA12 Notebook PDF

The document discusses nested loops in Python. A nested loop is when one loop is placed inside another loop. The inner loop will run completely for each iteration of the outer loop. Several examples are provided that use nested for loops to print out values in various patterns to demonstrate how nested loops work.

Uploaded by

Villa Vanitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views7 pages

BA12 Notebook PDF

The document discusses nested loops in Python. A nested loop is when one loop is placed inside another loop. The inner loop will run completely for each iteration of the outer loop. Several examples are provided that use nested for loops to print out values in various patterns to demonstrate how nested loops work.

Uploaded by

Villa Vanitha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

2/21/23, 3:21 PM BA12-Notebook - Jupyter Notebook

Nested Loop

A nested loop is a loop inside a loop.


The "inner loop" will be executed one time for each iteration of the "outer loop".

for element in sequence:


#inner for loop
for element in sequence:
body of inner for loop
body of outer for loop

In [ ]:

In [1]:

for i in range(3):
print('i = ', i)
for j in range(3):
print('j = ',j)

i = 0
j = 0
j = 1
j = 2
i = 1
j = 0
j = 1
j = 2
i = 2
j = 0
j = 1
j = 2

In [4]:

for i in range(5):
for j in range(5):
print("({}, {})".format(i,j), end=' ')
print(" ")

(0, 0) (0, 1) (0, 2) (0, 3) (0, 4)


(1, 0) (1, 1) (1, 2) (1, 3) (1, 4)
(2, 0) (2, 1) (2, 2) (2, 3) (2, 4)
(3, 0) (3, 1) (3, 2) (3, 3) (3, 4)
(4, 0) (4, 1) (4, 2) (4, 3) (4, 4)

localhost:8889/notebooks/Term4S/scripts/2A/BA12-Notebook.ipynb 1/7
2/21/23, 3:21 PM BA12-Notebook - Jupyter Notebook

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

In [13]:

for i in range(1,6):
for j in range(1,6):
print("{}".format(j), end=' ')
print(" ")

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

In [14]:

for i in range(1,6):
for j in range(1,6):
print("* ", end=' ')
print(" ")

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

In [10]:

for i in range(1,6):
for j in range(1,6):
print("{}".format(i), end=' ')
print(" ")

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

localhost:8889/notebooks/Term4S/scripts/2A/BA12-Notebook.ipynb 2/7
2/21/23, 3:21 PM BA12-Notebook - Jupyter Notebook

In [17]:

for i in range(1,6):
for j in range(1,6):
if i == j:
print("({},{})".format(i,j), end=' ')
print(" ")

(1,1)
(2,2)
(3,3)
(4,4)
(5,5)

In [19]:

for i in range(1,6):
for j in range(1,6):
if i + j == 6:
print("({},{})".format(i,j), end=' ')
print(" ")

(1,5)
(2,4)
(3,3)
(4,2)
(5,1)

(1,1)
(2,2)
(3,3)
(4,4)
(5,5)

In [45]:

for i in range(1, 6):


# inner loop
for j in range(1, 6):
print("({},{})".format(i,j), end=" ")
print('')

(1,1) (1,2) (1,3) (1,4) (1,5)


(2,1) (2,2) (2,3) (2,4) (2,5)
(3,1) (3,2) (3,3) (3,4) (3,5)
(4,1) (4,2) (4,3) (4,4) (4,5)
(5,1) (5,2) (5,3) (5,4) (5,5)

localhost:8889/notebooks/Term4S/scripts/2A/BA12-Notebook.ipynb 3/7
2/21/23, 3:21 PM BA12-Notebook - Jupyter Notebook

In [46]:

for i in range(1, 6):


# inner loop
for j in range(1, 6):
if i==j:
print("({},{})".format(i,j), end=" ")
print('')

(1,1)
(2,2)
(3,3)
(4,4)
(5,5)

(1,1)
(2,2)
(3,3)
(4,4)
(5,5)

In [ ]:

(1,5)
(2,4)
(3,3)
(4,2)
(5,1)

In [21]:

for i in range(1, 6):


# inner loop
for j in range(1, 6):
if i==j:
print("({},{})".format(i,j), end=" ")
else:
print(" ", end=" ")
print('')

(1,1)
(2,2)
(3,3)
(4,4)
(5,5)

localhost:8889/notebooks/Term4S/scripts/2A/BA12-Notebook.ipynb 4/7
2/21/23, 3:21 PM BA12-Notebook - Jupyter Notebook

(1,1)
(2,2)
(3,3)
(4,4)
(5,5)

In [ ]:

In [24]:

for i in range(1, 6):


for j in range(1, i+1):
print("{}".format(j), end=" ")
print('')

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

In [25]:

for i in range(5, 0,-1):


for j in range(1, i+1):
print("{}".format(j), end=" ")
print('')

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

In [28]:

for i in range(1, 6):


for j in range(1, i+1):
print("* ", end=" ")
print('')

*
* *
* * *
* * * *
* * * * *

localhost:8889/notebooks/Term4S/scripts/2A/BA12-Notebook.ipynb 5/7
2/21/23, 3:21 PM BA12-Notebook - Jupyter Notebook

In [33]:

for i in range(1, 6):


for j in range(1, i+1):
print(i, end=" ")
print('')

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

In [34]:

for i in range(1, 6):


for j in range(i,0,-1):
print(j, end=" ")
print('')

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

In [37]:

a = 1
for i in range(1, 6):
for j in range(i,0,-1):
print(a, end=" ")
a = a + 1
print('')

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

In [38]:

a = 1
for i in range(1, 6):
for j in range(1, i+1):
print(a, end=" ")
a=a+2
print('')

1
3 3
5 5 5
7 7 7 7
9 9 9 9 9

localhost:8889/notebooks/Term4S/scripts/2A/BA12-Notebook.ipynb 6/7
2/21/23, 3:21 PM BA12-Notebook - Jupyter Notebook

In [41]:

for i in range(1, 10, 2):


for j in range(1, i+1, 2):
print(i, end=" ")
print('')

1
3 3
5 5 5
7 7 7 7
9 9 9 9 9

In [45]:

for i in range(1, 6):


for k in range(5, i, -1):
print(" ", end=" ")
for j in range(1, i+1):
print("*", end=" ")
print('')

*
* *
* * *
* * * *
* * * * *

In [46]:

for i in range(1, 6):


for k in range(5, i, -1):
print(" ", end=" ")
for j in range(1, i+1):
print("*", end=" ")
for j in range(2, i+1):
print("*", end=" ")
print('')

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

In [ ]:

localhost:8889/notebooks/Term4S/scripts/2A/BA12-Notebook.ipynb 7/7

You might also like