Skip to content

Commit 00302e5

Browse files
committed
Python : Updated Minor Assignment 4
1 parent 6d189f8 commit 00302e5

File tree

1 file changed

+21
-0
lines changed
  • 5. Fifth_Semester/PYTHON/Minor Assignment 4

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'''
2+
Ques 12: Define a function that returns the sum of all the elements in a specified column in a matrix. Write a
3+
Python program that reads a 3-by-4 matrix and displays the sum of each column. Here is a sample
4+
run:
5+
Enter a 3-by-4 matrix row by row:
6+
1.5 2 3 4
7+
5.5 6 7 8
8+
9.5 1 3 1
9+
'''
10+
11+
def column_sum(matrix, col_index):
12+
return sum(row[col_index] for row in matrix)
13+
14+
matrix = []
15+
print("Enter a 3-by-4 matrix row by row:")
16+
for _ in range(3):
17+
row = list(map(float, input().split()))
18+
matrix.append(row)
19+
20+
for col in range(4):
21+
print(f"Sum of column {col + 1}: {column_sum(matrix, col)}")

0 commit comments

Comments
 (0)