0% found this document useful (0 votes)
25 views

Python Exp 7 - Merged

This document contains code for two Python programs that perform matrix operations. The first program uses NumPy to create and manipulate matrices. It allows the user to create matrices, print them, find the transpose, add matrices, and multiply matrices. The second program performs the same operations without using NumPy by manually handling the matrix elements and calculations. Both programs provide a menu for the user to select the desired operation.

Uploaded by

jnkn
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)
25 views

Python Exp 7 - Merged

This document contains code for two Python programs that perform matrix operations. The first program uses NumPy to create and manipulate matrices. It allows the user to create matrices, print them, find the transpose, add matrices, and multiply matrices. The second program performs the same operations without using NumPy by manually handling the matrix elements and calculations. Both programs provide a menu for the user to select the desired operation.

Uploaded by

jnkn
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/ 13

Department of Information Technology

Semester S.E. Semester IV – INFT

Subject Python Programming Lab (SBL)

Laboratory Shruti Agrawal


Teacher:

Laboratory -

Student Name Audumbar Gutte

Roll Number 21101B0016

Grade and
Subject Teacher’s
Signature

Experiment 06
Number

Problem Write a python program to search an element in 1D Array.


Statement

Resources / Hardware: Desktop/Laptop Software: Google Colaboratory


Apparatus
Required

Code:
import array as arr
found=0
count=0
sizeo = int(input("Enter number of elements of your
array : "))
a=arr.array('i',[])
while(sizeo > 0):
enter = int(input())
a.append(enter)
sizeo = sizeo -1
b = int(input("Enter element to be searched : "))
for i in a:
count+=1
if b == i:
found=1
break

if found == 1:
print("Element found at",count,"position")
else:
print("Element not found")
Output:
Department of Information Technology

Semester S.E. Semester IV – INFT

Subject Python Programming Lab (SBL)

Laboratory Shruti Agrawal


Teacher:

Laboratory -

Student Name Audumbar Gutte

Roll Number 21101B0016

Grade and
Subject Teacher’s
Signature

Experiment 07
Number

Problem Write a python program to accept two matrices and find their tanspose,
Statement addition and product.

Resources / Hardware: Desktop/Laptop Software: Google Colaboratory


Apparatus
Required

Code: #Code using inbuilt functions


from numpy import *
arr= []
def check_size(b):
if b <= len(arr):
return True

while True:
print("MENU\n1.Create new Matrix\n2.Print
Matrix\n3.Transpose of Matrix\n4.Addition of
Matrix\n5.Multiplication of Matrix\n6.Delete
Matrix\n7.Exit")
a=int(input("Enter Choice:"))
if a==1:
r=int(input("Enter number of rows:"))
c=int(input("Enter number of columns:"))
mat=array([[0]*c]*r)
for i in range(0,r):
for j in range(0,c):
mat[i][j]=float(input("Enter the element
"+str(i)+" "+str(j)+" : "))
arr.append(mat)
print(arr)
elif a==2:
for i in range(1,len(arr)+1):
print(i)
b=int(input("Select Matrix:"))
if check_size(b):
print(arr[b-1])
else:
print("Matrix "+str(b)+" does not exist")
elif a==3:
for i in range(1,len(arr)+1):
print(i)
b=int(input("Select Matrix:"))
if check_size(b):
print(arr[b-1].transpose())
else:
print("Matrix "+str(b)+" does not exist")
elif a==4:
for i in range(1,len(arr)+1):
print(i)
b1=int(input("Select 1st Matrix: "))
b2=int(input("Select 2st Matrix: "))
if check_size(b1):
if check_size(b2):
if arr[b1-1].shape[0]==arr[b2-1].shape[0] and
arr[b1-1].shape[1]==arr[b2-1].shape[1]:
print(arr[b1-1]+arr[b2-1])
else:
print("Cannot add the chosen matrix due to
mismatch of orders of the matrix!")
else:
print("Matrix "+str(b2)+" does not exist")
else:
print("Matrix "+str(b1)+" does not exist")
elif a==5:
for i in range(1,len(arr)+1):
print(i)
b1=int(input("Select 1st Matrix: "))
b2=int(input("Select 2st Matrix: "))
if check_size(b1):
if check_size(b2):
if arr[b1-1].shape[1]==arr[b2-1].shape[0]:
print(dot(arr[b1-1],arr[b2-1]))
else:
print("Cannot multiply the chosen matrix due
to mismatch of orders of the matrix!")
else:
print("Matrix "+str(b2)+" does not exist")
else:
print("Matrix "+str(b1)+" does not exist")
elif a==6:
for i in range(1,len(arr)+1):
print(i)
b=int(input("Select Matrix: "))
if check_size(b):
print("Matrix deleted is",arr.pop(b-1))
else:
print("Matrix "+str(b)+" does not exist")
print(arr)
elif a==7:
break

#Code without using inbuilt functions


from numpy import *

ch = 0
while (ch != 4):
print('MENU\n1)Transpose of matrix\n2)Addition of
matrix\n3)Multiplication of matrix\n4)Exit')
ch = int(input(':'))

if ch ==1:
a = int(input('Enter no. of rows : '))
b = int(input('Enter no. of columns : '))
arr = array([[0]*b]*a)
arr1 = array([[0]*a]*b)
for i in range (a):
for j in range (b):
c = int(input('Enter element:'))
arr[i][j] = c
print(arr)
print('Transpose of your matrix is : ')
for i in range (a):
for j in range (b):
arr1[j][i] = arr[i][j]
print(arr1)

elif ch == 2:
a = int(input('Enter no. of rows for matrix 1
: '))
b = int(input('Enter no. of columns for matrix
1 : '))
arr = array([[0]*a]*b)
p = int(input('Enter no. of rows for matrix 2
: '))
q = int(input('Enter no. of columns for matrix
2 : '))
arr1 = array([[0]*p]*q)
arr2 = array([[0]*a]*b)
if (a != p or b != q):
print('Dimensions of matrices are not same')
else:
for i in range (a):
for j in range (b):
c = int(input('Enter element for matrix
1:'))
arr[i][j] = c
print(arr)
for i in range (a):
for j in range (b):
c = int(input('Enter element for matrix
2:'))
arr1[i][j] = c
print(arr1)
for i in range (a):
for j in range (b):
arr2[i][j] = arr[i][j] + arr1[i][j]
print('addition of matrix is: /n')
print(arr2)

elif ch == 3:
a = int(input('Enter no. of rows for matrix 1
: '))
b = int(input('Enter no. of columns for matrix
1 : '))
arr = array([[0]*a]*b)
p = int(input('Enter no. of rows for matrix 2
: '))
q = int(input('Enter no. of columns for matrix
2 : '))
arr1 = array([[0]*p]*q)
arr2 = array([[0]*a]*b)
if (b != p):
print('Dimensions of matrices are not
compatible')
else:
for i in range (a):
for j in range (b):
c = int(input('Enter element for matrix
1:'))
arr[i][j] = c
print(arr)
for i in range (a):
for j in range (b):
c = int(input('Enter element for matrix
2:'))
arr1[i][j] = c
print(arr1)
for i in range (len(arr)):
for j in range (len(arr1[0])):
for k in range (len(arr1)):
arr2[i][j] += arr[i][k] * arr1[k][j]
print('Multiplication of matrix is: /n')
print(arr2)

elif ch == 4:
print('you have exited successfully\n')
Output:
#second code

You might also like