In: Computer Science
For this lab, you will work with two-dimensional lists in Python. Do the following:
def sumColumn(matrix, columnIndex)
2.5 3.0 4.0 1.5 1.5 4.0 2.0 7.5 3.5 1.0 1.0 2.5
def getMatrix(numRows, numColumns):
    '''returns a matrix with the specified number of rows and columns'''
    m = [] # the 2D list (i.e., matrix), initially empty
    for row in range(numRows):
        matrix_dimensions_string = str(numRows) + "-by-" + str(numColumns)
        line = input("Enter a " + matrix_dimensions_string + " matrix row for row " + str(row) + ": ")
    
        
    return m    
write a function called getMatrix(numRows,
numColumns) that returns a matrix with
numRows rows and numColumns
columns. The function reads the values of the matrix from the user
on a row by row basis. The values of each row must be
entered on one line using a space to separate the values. Use loops
in your solution. A sample run of this function when it is
caled as getMatrix(3, 4) to return a 3 by 4 matrix
is as follows. Values shown in red are entered by the user:
Enter a 3-by-4 matrix row for row 0: 2.5 3 4 1.5 Enter a 3-by-4 matrix row for row 1: 1.5 4 2 7.5 Enter a 3-by-4 matrix row for row 2: 3.5 1 1 2.5
Use the following function to test your code:
def main():
    m = getMatrix(3, 4)
    print("\nThe matrix is")
    display(m)
    print()
    for col in range(len(m[0])):
        print("Sum of elements for column %d is %.2f" % (col, sumColumn(m,col)))          
main()
A sample program run is as follows:
Enter a 3-by-4 matrix row for row 0: 1 22 3.7869 8 Enter a 3-by-4 matrix row for row 1: 4 5.6543 3 3 Enter a 3-by-4 matrix row for row 2: 1 1 1 1 The matrix is 1.0 22.0 3.7869 8.0 4.0 5.6543 3.0 3.0 1.0 1.0 1.0 1.0 Sum of elements for column 0 is 6.00 Sum of elements for column 1 is 28.65 Sum of elements for column 2 is 7.79 Sum of elements for column 3 is 12.00
The format of the input and output of your program must be as in the sample run.
Program
def getMatrix(numRows, numColumns):
    '''returns a matrix with the specified number of
rows and columns'''
    m = [] # the 2D list (i.e., matrix), initially
empty
    for row in range(numRows):
        matrix_dimensions_string
= str(numRows) + "-by-" + str(numColumns)
        line = input("Enter a "
+ matrix_dimensions_string + " matrix row for row " + str(row) + ":
")
        row = line.split('
')
        row = [float(i) for i in
row]
        m.append(row)
      
      
    return m
def display(m):
    for row in m:
        print ("
".join([str(col) for col in row]))
      
def sumColumn(matrix, columnIndex):
    return sum([row[columnIndex] for row in
matrix])
def main():
    m = getMatrix(3,4)
    print("\nThe matrix is")
    display(m)
    print()
    for col in range(len(m[0])):
        print("Sum of elements
for column %d is %.2f" % (col,
sumColumn(m,col)))        
main()
Output
Enter a 3-by-4 matrix row for row 0: 1 22 3.7869 8
Enter a 3-by-4 matrix row for row 1: 4 5.6543 3 3
Enter a 3-by-4 matrix row for row 2: 1 1 1 1
The matrix is
1.0 22.0 3.7869 8.0
4.0 5.6543 3.0 3.0
1.0 1.0 1.0 1.0
Sum of elements for column 0 is 6.00
Sum of elements for column 1 is 28.65
Sum of elements for column 2 is 7.79
Sum of elements for column 3 is 12.00
Screenshot
