0% found this document useful (0 votes)
15 views6 pages

10 Nov Nested 4

The document contains examples of nested loops in Python that demonstrate printing patterns using characters like *, numbers, or spaces. Some examples include printing triangles, diagonal lines, and grids using nested loops and conditionals to control the printing on each line.
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)
15 views6 pages

10 Nov Nested 4

The document contains examples of nested loops in Python that demonstrate printing patterns using characters like *, numbers, or spaces. Some examples include printing triangles, diagonal lines, and grids using nested loops and conditionals to control the printing on each line.
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/ 6

11/29/22, 10:23 AM 10 nov nested 4 - Jupyter Notebook

1 # Nested Loops and Sequence


In [3]:

for i in range(0,6):
print(i)

In [10]:

for i in range(9):
for j in range(1,9):
if j<=i:
print ("*",end="....")
print()

*....

*....*....

*....*....*....

*....*....*....*....

*....*....*....*....*....

*....*....*....*....*....*....

*....*....*....*....*....*....*....

*....*....*....*....*....*....*....*....

In [11]:

for i in range(9):
for j in range(1,9):
if j<=i:
print (j,end=" ")
print()

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

1 2 3 4 5 6 7

1 2 3 4 5 6 7 8

localhost:8888/notebooks/10 nov nested 4.ipynb 1/6


11/29/22, 10:23 AM 10 nov nested 4 - Jupyter Notebook

In [13]:

for i in range(9):
for j in range(1,9):
if j<=i:
print (i,end=" ")
print()

2 2

3 3 3

4 4 4 4

5 5 5 5 5

6 6 6 6 6 6

7 7 7 7 7 7 7

8 8 8 8 8 8 8 8

In [17]:

for i in range(9):
for j in range(1,9):
if j<=5:
print ("*",end=" ")
print()

* * * * *

* * * * *

* * * * *

* * * * *

* * * * *

* * * * *

* * * * *

* * * * *

* * * * *

In [21]:

for i in range(9):
for j in range(1,9):
if j<=i:
print ("*",end=".|.|.|.")
print()

*.|.|.|.

*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

localhost:8888/notebooks/10 nov nested 4.ipynb 2/6


11/29/22, 10:23 AM 10 nov nested 4 - Jupyter Notebook

In [22]:

for i in range(9):
for j in range(1,9):
if j<=5:
print (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

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

1 2 3 4 5

In [24]:

for i in range(9):
for j in range(1,9):
if j<=i:
print ("*",end="!!!!!!")
print()

*!!!!!!

*!!!!!!*!!!!!!

*!!!!!!*!!!!!!*!!!!!!

*!!!!!!*!!!!!!*!!!!!!*!!!!!!

*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!

*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!

*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!

*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!*!!!!!!

In [25]:

for i in range(9):
for j in range(1,9):
if j<=i:
print ("*",end=".|.|.|.@")
print()

*.|.|.|.@

*.|.|.|.@*.|.|.|.@

*.|.|.|.@*.|.|.|.@*.|.|.|.@

*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@

*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@

*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@

*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@

*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@*.|.|.|.@

localhost:8888/notebooks/10 nov nested 4.ipynb 3/6


11/29/22, 10:23 AM 10 nov nested 4 - Jupyter Notebook

In [26]:

for i in range(9):
for j in range(1,9):
if j<=i:
print ("*",end=".|.|.|.")
print()

*.|.|.|.

*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.*.|.|.|.

In [42]:

for i in range(7):
for j in range(0,6):
if j<i:
print (6-j,end=" ")
print()

6 5

6 5 4

6 5 4 3

6 5 4 3 2

6 5 4 3 2 1

In [36]:

for i in range(9):
for j in range(7,0,-1):
if j<i:
print (j,end=" ")
print()

2 1

3 2 1

4 3 2 1

5 4 3 2 1

6 5 4 3 2 1

7 6 5 4 3 2 1

localhost:8888/notebooks/10 nov nested 4.ipynb 4/6


11/29/22, 10:23 AM 10 nov nested 4 - Jupyter Notebook

In [43]:

for i in range(9):
for j in range(1,9):
if j<=i:
print (j,end=" ")
print()

1 2

1 2 3

1 2 3 4

1 2 3 4 5

1 2 3 4 5 6

1 2 3 4 5 6 7

1 2 3 4 5 6 7 8

In [1]:

for i in range(9):
for j in range(1,9):
if j>i:
print (j,end=" ")
print()

1 2 3 4 5 6 7 8

2 3 4 5 6 7 8

3 4 5 6 7 8

4 5 6 7 8

5 6 7 8

6 7 8

7 8

In [2]:

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

print(" ")

* *

* * *

* * * *

* * * * *

localhost:8888/notebooks/10 nov nested 4.ipynb 5/6


11/29/22, 10:23 AM 10 nov nested 4 - Jupyter Notebook

In [12]:

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

print(" ")

2 2

3 3 3

4 4 4 4

5 5 5 5 5

In [13]:

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

print(" ")

1 2

1 2 3

1 2 3 4

1 2 3 4 5

localhost:8888/notebooks/10 nov nested 4.ipynb 6/6

You might also like