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

Python

Nishant Parwani

Uploaded by

nishantparwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Python

Nishant Parwani

Uploaded by

nishantparwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

"""1.

Generate a Random Number using random(), randint(), sample()


[import random]
2. Create a 1D array by reading elements from user
3. Create a 2D array by reading elements from user
4. Create 2D array of size mXn with random number between p & q.
5. Display shape, size, dimension etc of above array
6. Reshape the 2D array in dimension (pXq)
7. Perform slicing on 1D & 2D array
8. Sorting operation on 1D array.
9. Write a Python program to sort a given array of shape 2 along the
first axis, last axis and on the flattened array
10. Apply NumPy to perform following operations on the 2D arrays
a. Addition
b. Multiplication
c. Transpose
d. Inverse
e. Multiply 2D array by scalar
11. Write a program to find multiplication of matrix using nested
loop. """

#1. Generate a Random Number using random(), randint(), sample()


[import random]
import random
lb=int(input("Enter a lower limit:"))
ub=int(input("Enter an upper limit:"))
ran=random.randint(lb,ub)
print("The random Number is:", ran)
ran1=random.random()
print("The random Number is:", ran1)
k=[1,2,3,5]
print("A sample list:",k)
ran2=random.sample(k,2)
print("The random Number is:", ran2)

Enter a lower limit: 10


Enter an upper limit: 20

The random Number is: 19


The random Number is: 0.7540027941391753
A sample list: [1, 2, 3, 5]
The random Number is: [1, 5]

#2. Create a 1D array by reading elements from user


import numpy as np
el = int(input("Enter the number of elements in the array: "))
k= []
for i in range(el):
ele = float(input("Enter element : "))
k.append(ele)
array = np.array(k)
print("The required array is:", array)

Enter the number of elements in the array: 4


Enter element : 1
Enter element : 2
Enter element : 3
Enter element : 4

The required array is: [1. 2. 3. 4.]

#3. Create a 2D array by reading elements from user


import numpy as np
el = int(input("Enter the number of rows in the array: "))
ele = int(input("Enter the number of columns in the array: "))
k = []
for i in range(el):
row = []
for j in range(ele):
q = float(input("Enter element : "))
row.append(q)
k.append(row)
array = np.array(k)
print("The required array is: \n",array)

Enter the number of rows in the array: 2


Enter the number of columns in the array: 2
Enter element : 1
Enter element : 2
Enter element : 3
Enter element : 4

The required array is:


[[1. 2.]
[3. 4.]]

#4. Create 2D array of size mXn with random number between p & q.
p=int(input("Enter a lower limit:"))
q=int(input("Enter an upper limit:"))
m=random.randint(p,q)
n=random.randint(p,q)
print("So the required matrix",m,"x",n)
import numpy as np
k = []
for i in range(m):
row = []
for j in range(n):
q = float(input("Enter element : "))
row.append(q)
k.append(row)
array = np.array(k)
print("The required array is: \n",array)

Enter a lower limit: 2


Enter an upper limit: 5

So the required matrix 3 x 3

Enter element : 1
Enter element : 2
Enter element : 3
Enter element : 4
Enter element : 5
Enter element : 6
Enter element : 7
Enter element : 8
Enter element : 9

The required array is:


[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]

#5. Display shape, size, dimension etc of above array


import numpy as np
print("Array: \n",array)
print("\nShape of the array:", array.shape)
print("Size of the array:", array.size)
print("Dimensions of the array:", array.ndim)

Array:
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]

Shape of the array: (3, 3)


Size of the array: 9
Dimensions of the array: 2

#6. Reshape the 2D array in dimension (pXq)


p=int(input("Enter no of rows:"))
q=int(input("Enter no of columns:"))
print("So the required matrix",p,"x",q)
import numpy as np
k = []
g=p*q
for i in range(g):
m = int(input("Enter element : "))
k.append(m)
array = np.array(k)
array = array.reshape(p, q)
print("The required array is: \n",array)

Enter no of rows: 3
Enter no of columns: 3

So the required matrix 3 x 3

Enter element : 1
Enter element : 2
Enter element : 3
Enter element : 4
Enter element : 5
Enter element : 6
Enter element : 7
Enter element : 8
Enter element : 9

The required array is:


[[1 2 3]
[4 5 6]
[7 8 9]]

#7. Perform slicing on 1D & 2D array


import numpy as np
d = [1, 2, 3, 4]
array = np.array(d)
print(d_array[:])
dd = np.array([[11, 22], [33, 44]])
print(dd[::])

[1 2 3 4]
[[11 22]
[33 44]]

#8. Sorting operation on 1D array.


import numpy as np
d = [1,5, 2, 3, 4]
array = np.array(d)
print(np.sort(array))

[1 2 3 4 5]

#9. Write a Python program to sort a given array of shape 2 along the
first axis, last axis and on the flattened array
import numpy as np
a=np.array([[11,22] , [33,44]])
print("Sorting on first axis")
print(np.sort(a,axis=0))
print("Sorting on last axis")
print(np.sort(a, axis=1))
print("Sort the flattened array")
print(np.sort(a,axis=None))

Sorting on first axis


[[11 22]
[33 44]]
Sorting on last axis
[[11 22]
[33 44]]
Sort the flattened array
[11 22 33 44]

"""10. Apply NumPy to perform following operations on the 2D


arrays
a. Addition
b. Multiplication
c. Transpose
d. Inverse
e. Multiply 2D array by scalar"""
import numpy as np
q = np.array([[1, 2], [3, 4]])
p = np.array([[5, 6], [7, 8]])

while True:
print("*****Menu*****")
print("1. Addition of two 2D arrays")
print("2. Multiplication of two 2D arrays")
print("3. Transpose of a 2D array")
print("4. Inverse of a 2D array")
print("5. Multiply 2D array by scalar")
print("6. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:
print("Addition of arrays:")
res = np.add(q, p)
print(res)

elif choice == 2:
print("Multiplication of arrays:")
result = np.multiply(q, p)
print(result)

elif choice == 3:
print("Transpose of the first array:")
res = np.transpose(q)
print(res)
print("Transpose of the second array:")
res = np.transpose(p)
print(res)

elif choice == 4:
print("Inverse of the first array:")
res = np.linalg.inv(q)
print(res)
print("Inverse of the second array:")
res = np.linalg.inv(p)
print(res)

elif choice == 5:
sc = float(input("Enter a scalar value: "))
print("Multiplying the first array by scalar:")
result = np.multiply(q, sc)
print(result)
print("Multiplying the second array by scalar:")
result = np.multiply(p, sc)
print(result)

elif choice == 6:
print("Exiting the program.")
break
else:
print("Invalid choice. Please try again.")

*****Menu*****
1. Addition of two 2D arrays
2. Multiplication of two 2D arrays
3. Transpose of a 2D array
4. Inverse of a 2D array
5. Multiply 2D array by scalar
6. Exit

Enter your choice: 1

Addition of arrays:
[[ 6 8]
[10 12]]
*****Menu*****
1. Addition of two 2D arrays
2. Multiplication of two 2D arrays
3. Transpose of a 2D array
4. Inverse of a 2D array
5. Multiply 2D array by scalar
6. Exit

Enter your choice: 2


Multiplication of arrays:
[[ 5 12]
[21 32]]
*****Menu*****
1. Addition of two 2D arrays
2. Multiplication of two 2D arrays
3. Transpose of a 2D array
4. Inverse of a 2D array
5. Multiply 2D array by scalar
6. Exit

Enter your choice: 6

Exiting the program.

#11. Write a program to find multiplication of matrix using nested


loop.
a=[[1,2,3],[4,5,6],[7,8,9]]
b=[[1,2,3],[4,5,6],[7,8,9]]
result=[[0,0,0],[0,0,0],[0,0,0]]
for i in range(len(a)):
for j in range(len(b[0])):
for k in range(len(b)):
result[i][j] += a[i][k]*b[k][j]
print("Resultant matrix is:")
for r in result:
print(r)

Resultant matrix is:


[30, 36, 42]
[66, 81, 96]
[102, 126, 150]

"""CONCLUSION: Upon Performing this experiment we were able to


understand the difference tools and fucntions that can be performed
upon arrays and
also different functions that are covered under the numpy module. We
also got to learn different identities on matrices.ALSO we got to
learn random
module. """

'CONCLUSION: Upon Performing this experiment we were able to


understand the difference tools and fucntions that can be performed
upon arrays and\n also different functions that are covered under the
numpy module. We also got to learn different identities on
matrices.ALSO we got to learn random \n module. '

You might also like