Python program to Interchange the Principal Diagonals of Matrix

from random import randint
row = 5
matrix = []
for i in range(row):
 myrow = []
 for j in range(row):
     myrow.append(randint(1,100))
     print("%4d" % myrow[j], end=' ')
 matrix.append(myrow)
 print()
print()
print("Interchanged principal diagonals of matrix below")
for i in range(row):
 x = matrix[i][i]
 matrix[i][i] = matrix[i][row-1-i]
 matrix[i][row-1-i] = x
for i in matrix:
 for j in i:
    print("%4d" % j, end=' ')
 print()

 

 

Output:

  84   18   75   69   58
  73   33   20    7   75
  35   50    1   24   31
   8   50   75   10   98
  49   73   60   97    5

 

Subscribe For Daily Updates

100+ Python Pattern Examplespython pattern examples - star patterns, number patterns