Programming With Python Practical 13

Question 1
Write a Python program to create two matrices and perform addition, subtraction, multiplication and division operation on matrix.
matrix.py
import numpy as np
a=np.array([])
b=np.array([])

print("Enter elements of first matrix: ")
for i in range(0,4):
        num=int(input("Element:"))
        a=np.append(a,num)
a.shape=(2,2)

print("Enter elements of second matrix: ")
for i in range(0,4):
        num=int(input("Element:"))
        b=np.append(b,num)
b.shape=(2,2)

print ("Addition of matrix is : ") 
print (np.add(a,b))

print ("Subtraction of matrix is : ") 
print (np.subtract(a,b))

print ("Division of matrix is : ") 
print (np.divide(a,b))

print ("Multiplication of matrix is : ") 
print (np.dot(a,b))
Output
Question 2
Write a Python program to concatenate two strings.
concat.py
import numpy as np

strr=input("Enter first string: ")
str1=np.array(strr,dtype=str)

strr=input("Enter second string: ")
str2=np.array(strr,dtype=str)

newstr=np.char.add(str1,str2)
print("Concated string is: ",newstr)
Output
Question 3
Write a NumPy program to generate six random integers between 10 and 30.
randomint.py
import numpy as np

print("Random numbers between 10 and 30:")
for i in range(0,6):
	print(np.random.randint(10,30))
Output

Leave a Comment

Your email address will not be published. Required fields are marked *