Python program to Sum of Items in Rows and Columns of Elements

from random import randint
col = 6
row = 6
matrix = []
sum_col = [0]*col
sum_row = [0]*row
for i in range(row):
 myrow = []
 for j in range(col):
    myrow.append(randint(0,3))
 matrix.append(myrow)
for i in range(row):
 for j in range(col):
     sum_row[i] += matrix[i][j]
     sum_col[j] += matrix[i][j]
for i in range(row):
 print(matrix[i], "|", sum_row[i])
print("_" *col*4)
print(sum_col)

 

 

Output:

[2, 3, 2, 0, 0, 2] | 9
[3, 1, 0, 1, 1, 3] | 9
[0, 3, 2, 0, 1, 2] | 8
[0, 0, 3, 0, 0, 3] | 6
[1, 1, 2, 2, 1, 2] | 9
[0, 3, 3, 1, 0, 2] | 9
________________________
[6, 11, 12, 4, 3, 14]

 

Subscribe For Daily Updates

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