Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Python Assignment 3

The document contains 3 examples of matrix operations in Python: 1) Adding two matrices, 2) Multiplying two matrices, and 3) Transposing a matrix. Each example shows the user input, matrix definitions and values, the operation, and the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Python Assignment 3

The document contains 3 examples of matrix operations in Python: 1) Adding two matrices, 2) Multiplying two matrices, and 3) Transposing a matrix. Each example shows the user input, matrix definitions and values, the operation, and the output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name:Yogesh

Reg no:20baa7008

1) Rows=int(input(‘enter the number of rows’:))


Column=int(input(‘enter the number of column:’))
Print(‘enter the elements of matrix’)
Matrix_a=[[int(input()) for I in range(column)] for I in range(rows)]
Print(‘first matrix is:’)
For n in matrix_a:
Print(n)

Print(‘enter the elements of second matrix’)


Matrix_b=[[int(input()) for I in range(column)] for I in range(rows)]
Print(‘first matrix is:’)
For n in matrix_b:
Print(n)

Result=[[0 for in range(column)] for I in range (rows)]


For I in range(rows):
For j in range(column):
Result[i][j]=matrix_a[i][j]+matrix_b[i][j]
Print(‘the sum of two above matrices is:’)
For r in result:
Print(r)

Output:
Enter the number of rows=3
Enter the number of columns=3
Enter the elements of matrix
1
2
3
4
5
6
7
8
9
First matrix is:
[1,2,3]
[4,5,6]
[7,8,9]

Enter the elements of second matrix


10
11
12
13
14
15
16
17
18
[10,11,12]
[13,14,15]
[16,17,18]
The sum of two above matrices is:
[11,13,15]
[17,19,21]
[23,25,27]

2) P=int(input(‘enter the row number for matrix1:’))


Q=int(input(‘enter the column number for matrix2:’))
N=int(input(‘enter the column number for matrix1/row number for matrix2:’))
Print(‘enter the elements for matrix1’)
Matrix1=[[int(input()) for I in range(n)] for j in range(p)]
Print(‘matrix1:’)
For I in range(p):
For j in range(n):
Print(format(matrix1[i][j],”<3”),end=“”)
Print()

Print(‘enter the elements for matrix2:’)


Matrix2=[[int(input()) for I in range(q)] for j in range(n)]
Print(‘matrix2:’)
For I in range(n):
For j in range(q):
Print(format(matrix2[i][j],”<3”),end=“”)
Print()
Result=[[0 for I in range(q)] for j in range (p)]
For I in range(p):
For j in range(q):
For k in range(n):
Result[i][j]=result[i][j]+matrix1[i][j]*matrix2[i][j]
Print(‘the multiplication of two matrices are:’)
For I in range(p):
For j in range(q):
Print(format(result[i][j],”<5”,)end=“”)
Print()

Output:
Enter the row number for matrix1=3
Enter the column number for matrix2=3
Enter the column number for matrix1/row number for matrix2:3
Enter the elements for matrix 1
1
2
3
4
5
6
7
8
9
Matrix1:
[1,2,3]
[4,5,6]
[7,8,9]
Enter the elements for matrix2
10
11
12
13
14
15
16
17
18
Matrix2:
[10,11,12]
[13,14,15]
[16,17,18]
The multiplication of two matrices are:
[84,90,96]
[201,216,231]
[318,342,366]

3)p=int(input(‘enter the number of rows:’))


Q=int(input(‘enter the number of columns:’))
Print(‘enter the elements fo matrix1’)
Matrix1=[[int(input()) for I in range(q)] for j in range(p)]
Print(‘matrix1:’)
For I in range(p):
For j in range(q):
Print(format(matrix1[i][j],”<4”),end=“”)
Print()
Result=[[0 for I in range(p)] for j in range(q)]
For I in range(p):
For j in range(q):
Result[i][j]=matrix1[j][i]
For I in range(q):
For j in range(p):
Print(format(result[i][j],”<4”),end=“”)
Print()

Output:
Enter the number of rows:3
Enter the number of columns:3
Enter the elements for th matrix 1
1
2
3
4
5
6
7
8
9
Matrix1:
[1,2,3]. [1,4,7]
[4,5,6]. [2,5,8]
[7,8,9] [3,6,9]

You might also like