Matrix Pattern in Python language using For loop
ADVERTISEMENTS
Matrix pattern in python language using for loop. In this article, you will learn how to print the matrix pattern in python language using the for loop.
Source Code
# Matrix Pattern in Python language using For loop
x, i, j, k, d, m = None, None, None, None, 1, None
# x - denotes the number of rows
print ("-----Enter the total number of rows-----")
x = int (input ())
mat = []
for i in range (x):
j = i
k = 0
for j in range (i, -1, -1):
mat.append([])
mat[k].append(d)
d = d + 1
k = k + 1
for k in range (1, x):
i = m = k
for j in range (x - 1, m - 1, -1):
mat[i].append(d)
d = d + 1
i = i + 1
print ("\n-----The matrix pattern-----\n")
for i in range (x):
print (end="\t")
for j in range (x):
print (mat[i][j], end="\t")
print (end="\n\n")
Output
-----Enter the total number of rows-----
6
-----The matrix pattern-----
1 2 4 7 11 16
3 5 8 12 17 22
6 9 13 18 23 27
10 14 19 24 28 31
15 20 25 29 32 34
21 26 30 33 35 36