python pattern programming
PYTHON PATTERN PROGRAMMING
Python nested loop:
Python allow programmer to use nested loops.Nested loop means when one loop is define into another loop is known as nested loop,in which one is called inner loop and another is called outer loop.In which when outer loop's condition is true than inner loop will be execute.
we use the concept of nested loop in pattern programming etc.
we use the concept of nested loop in pattern programming etc.
syntax:
while(condition):
while(condition):
statement x
statement y
Some examples of nested loop.
Nested loop program.
Q. write a program to print following pattern.
P
P Y
P Y T
P Y T H
P Y T H O
P Y T H O N
str="PYTHON"
for i in range(0,6):
print()
for j in range(0,i+1):
print(str[j], end=' ')
*
* *
* * *
* * * *
* * * * *
for i in range(1,6):
print()
for j in range(i):
print("*", end= ' ')
Q. write a program to print following pattern.
1
1 2
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3 4 5
for i in range(1,6):
print()
for j in range(1,i+1):
print(j, end=' ')
Q. write a program to print following pattern.
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
for i in range(1,6):
print()
for j in range(1,i+1):
print(i, end=' ')
Q. write a program to print following pattern.
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
N = 5
for i in range(1,N+1):
for k in range(N,i,-1):
print(" ",end=' ')
for j in range(1,i+1):
print(j, end=' ')
for l in range(i-1,0,-1):
print(l,end=' ')
print()
Q. write a program to print following pattern.
*
* *
* *
* *
* *
* *
*
for row in range(7):
for col in range(7):
if(row+col==3 or col-row==3 or row-col==3 or row+col==9 ):
print("*", end="")
else:
print(end=" ")
print()
Q. write a program to print following pattern.
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
num=int(input("enter value of row :"))
n_list=[[0 for x in range(num)] for y in range(num)]
n=1
low=0
heigh=num-1
count=int(num+1/2)
for i in range(count):
for j in range(low,heigh+1):
n_list[i][j]=n
n=n+1
for k in range(low+1,heigh+1):
n_list[k][heigh]=n
n=n+1
for k in range(heigh-1,low-1,-1):
n_list[heigh][k]=n
n=n+1
for k in range(heigh-1,low,-1):
n_list[k][low]=n
n=n+1
low=low+1
heigh=heigh-1
for i in range(num):
for j in range(num):
print(n_list[i][j],end= "\t ")
print("\n\n")
Q.write a program to print the following pattern.
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
num=int(input("enter value of row :"))
n_list=[[0 for x in range(num)] for y in range(num)]
n=int((num+1)/2)
low=0
heigh=num-1
count=int((num+1)/2)
for i in range(count):
for j in range(low,heigh+1):
n_list[i][j]=n
for k in range(low+1,heigh+1):
n_list[k][heigh]=n
for k in range(heigh-1,low-1,-1):
n_list[heigh][k]=n
for k in range(heigh-1,low,-1):
n_list[k][low]=n
low=low+1
heigh=heigh-1
n=n-1
for i in range(num):
for j in range(num):
print(n_list[i][j],end= " ")
print("")
Thank you..........
please like and follow my facebook page.....
Let's_code
Decision making statement in python
Lopping_statement_in_python
function_in_python
Comments
Post a Comment