0% found this document useful (0 votes)
2 views4 pages

Python Nested Loops Examples

The document contains a series of Python code snippets demonstrating various loop structures and print formatting. It includes examples of nested loops to print patterns of numbers and asterisks, as well as user input handling for dynamic output. The code illustrates different ways to manipulate output formatting and control flow in Python.

Uploaded by

archanassingh24
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)
2 views4 pages

Python Nested Loops Examples

The document contains a series of Python code snippets demonstrating various loop structures and print formatting. It includes examples of nested loops to print patterns of numbers and asterisks, as well as user input handling for dynamic output. The code illustrates different ways to manipulate output formatting and control flow in Python.

Uploaded by

archanassingh24
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

01/07/2025, 15:30 Untitled51

In [12]:
i = 1
while i<=9:
z = 1

while z<=6:
print(z,end=" ") #end=" " same line print
z=z+1
print("")
i=i+1

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

In [18]:
print("1",end="-")
print("2",end=" ")
print("4")

1-2 4

In [21]:
i = 1
while i<=2:
z=1
while z<=4:
print(z,end=" ")
z=z+1
print("")
i=i+1

1 2 3 4
1 2 3 4

In [22]:
i = 1
while i<=2:
z=4
while z>=1:
print(z,end=" ")
z=z-1
print("")
i=i+1

4 3 2 1
4 3 2 1

In [23]:
i = 1
while i<=2:
z=4
while z>=1:
print("1",end=" ")
z=z-1
print("")
i=i+1

localhost:8888/nbconvert/html/[Link]?download=false 1/4
01/07/2025, 15:30 Untitled51
1 1 1 1
1 1 1 1

In [26]:
i = 1
while i<=8:
z=1
while z<=i:
print(z,end=" ")
z=z+1
print("")
i=i+1

1
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 [36]:
i = 1
while i<=8:
z=1
if i == 3 and i==5:
print("")
else:
while z<=8:
if (i==3 and z==4) or (i==3 and z==6) or (i==7 and z==2):
print(" ",end=" ")
else:
print("*",end=" ")
z=z+1
print("")
i=i+1

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

In [8]:
x = int(input("Enter any value"))
i = 1
while i<=x:
z=1
if i==1 or i==x:
while z<=x:
print("*",end=" ")
z=z+1
else:
n=1
while n<=x:
if n==1 or n==x:
print("*",end=" ")
else:
print(" ",end=" ")
n=n+1

localhost:8888/nbconvert/html/[Link]?download=false 2/4
01/07/2025, 15:30 Untitled51
print("")
i=i+1

Enter any value7


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

In [13]:
i = 1
while i<=8:
z=1
if i==8 or i==1:
while z<=8:
print("0",end=" ")
z=z+1

else:
while z<=8:
print("*",end=" ")
z=z+1
print("")
i=i+1

0 0 0 0 0 0 0 0
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
0 0 0 0 0 0 0 0

In [22]:
i = 1
while i<=4:

if i==2:
z=1
while z<=4:
print(" ",end=" ")
z=z+1
else:
z=1
while z<=4:
if i==3 and z==2:
print(" ",end=" ")
else:
print(i,end=" ")
z=z+1

print("")
i=i+1

1 1 1 1

3 3 3
4 4 4 4

localhost:8888/nbconvert/html/[Link]?download=false 3/4
01/07/2025, 15:30 Untitled51

In [ ]:
# Task 8 x 8
# Remove one line in each program
# Remove alternate lines

localhost:8888/nbconvert/html/[Link]?download=false 4/4

You might also like