0% found this document useful (0 votes)
21 views6 pages

Matrix

The document contains various code snippets demonstrating operations on matrices, including summing rows, columns, and diagonals, as well as performing element-wise operations. It also includes examples of printing patterns and calculating products of matrix elements. The output of each operation is shown, illustrating the results of the computations.

Uploaded by

sushantsaini3333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views6 pages

Matrix

The document contains various code snippets demonstrating operations on matrices, including summing rows, columns, and diagonals, as well as performing element-wise operations. It also includes examples of printing patterns and calculating products of matrix elements. The output of each operation is shown, illustrating the results of the computations.

Uploaded by

sushantsaini3333
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

matrix

March 1, 2025

[5]: li=[[1,3,4],[2,4,3],[1,2,5]]
li

[5]: [[1, 3, 4], [2, 4, 3], [1, 2, 5]]

[5]:

[7]: for i in range(0,3):


for j in range(0,3):
print(i,j,end=" | ")
print()

0 0 | 0 1 | 0 2 |
1 0 | 1 1 | 1 2 |
2 0 | 2 1 | 2 2 |

[ ]:

[25]: for i in range(0,3):


for j in range(0,3):
print(li[i][j],end=" | ")
print()

1 | 3 | 4 |
2 | 4 | 3 |
1 | 2 | 5 |

[ ]:

[121]: for i in range(0,3):


for j in range(0,1):
print(i,i)

0 0
1 1
2 2

[42]:

1
[196]: s=0

for j in range(0,1):
s=s+li[i][i]
print(s)

[ ]:

[ ]:

[235]: li=[[1,2,3],[4,5,6],[7,8,9]]
li

s=0
for i in range(0,3):
s=s+li[0][i]
print(s)
s=0
for i in range(0,3):
s=s+li[1][i]
print(s)
s=0

for i in range(0,3):
s=s+li[2][i]
print(s)
print()
s=0
for i in range(0,3):
s=s+li[i][0]
print(s,end=" ")
s=0
for i in range(0,3):
s=s+li[i][1]
print(s,end=" ")
s=0
for i in range(0,3):
s=s+li[i][2]
print(s)
print()
s=0
for i in range(0,3):
s=s+li[i][i]
print(s) #diagonal

2
print()
s=0
for i in range(0,3):
s=s+li[i][2-i]
print(s) #diagonal

6
15
24

12 15 18

15

15

[ ]:

[234]: x=[[1,2,3],[4,5,6],[7,8,9]]
y=[[2,1,1],[2,1,1],[1,2,2]]

s_x = 0
for i in range(0,3):
s_x += x[i][i]
print("Sum of diagonal of x: ",s_x)

s_y = 0
for j in range(0,3):
s_y += y[j][j]
print("Sum of diagonal of y: ",s_y)

totalsum=s_x+s_y
print("total sum of both diagonals",totalsum)

Sum of diagonal of x: 15
Sum of diagonal of y: 5
total sum of both diagonals 20

[ ]:

[281]: x=[[1,2],[4,1]]
y=[[2,2],[4,4]]

output=[]

for i in range(len(x)):
for j in range(len(x[i])):

3
output.append(x[i][j]**2 +y[i][j]**2)

print(output)

[5, 8, 32, 17]

[ ]:

[272]:

[308]: li=[[1,2,3],[4,5,6],[7,8,9]]
li
s=1

for i in range(len(li)):
for j in range(len(li[i])):
print(li[i][j]**2,end=" ")
print()
s *=li[i][j]**2
print("Multiply all of these squared values together. :",s)

1
4
9
16
25
36
49
64
81
Multiply all of these squared values together. : 131681894400

[288]:

[314]: li=[[1,2,3],[4,5,6],[7,8,9]]
li
s=1
for i in range(0,len(li)):
for j in range(0,len(li[i])):
s *= li[i][j]
print(s)

362880

[ ]:

4
[354]: li=[[1,2,3],[4,5,6],[7,8,9]]
li
s=1
for i in range(len(li)):
for j in range(len(li[i])):
if li[i][j] > 5:
s *= li[i][j]

print(s)

3024

[353]: li=[[1,2,3],[4,5,6],[7,8,9]]
li
s=0
for i in range(len(li)):
for j in range(len(li[i])):
if li[i][j] > 5:
s += li[i][j]

print(s)

30

[ ]:

[355]: for i in range(1,7):


for j in range(7,i,-1 ):
print(" ",end=" ")
for j in range(0,i):
if(i==3) and (j<=1) and (j>=1):
print(" ",end=" ")
elif(i==4) and (j>=1):
print("*",end=" ")
elif(i==5) and (j<=3) and (j>=3):
print(" ",end=" ")
else:
print("*",end=" ")
for j in range(1,i):

if(i==5) and (j>=1) and (j<=1):


print(" ",end=" ")
elif(i==4) and (j<=1):
print("*",end=" ")
elif(i==3) and (j<=1):
print(" ",end=" ")
else:

5
print("*",end=" ")
print()
for k in range(1,7):
for l in range(1,4 ):
print(" ",end=" ")
for l in range(1,8 ):
if(k>1 and k<6) and (l>1 and l<7):
print(" ",end=" ")
else:
print("*",end=" ")
print()

*
* * *
* * *
* * * * * * *
* * * * * * *
* * * * * * * * * * *
* * * * * * *
* *
* *
* *
* *
* * * * * * *

[ ]:

You might also like