Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Q1. Write a Python program to swap first and last element of the list.

def swap(list):
get = list[-1], list[0]
list[0], list[-1] = get
return list

ls = [12, 35, 9, 56, 24]


print("Original List:",(ls1))
print("Swapped List:",swap(ls))

Original List: [12, 35, 9, 56, 24]


Swapped List: [24, 35, 9, 56, 12]

Q3. Given a list of numbers, you need to write a Python program to find the second largest
number.
a=[10,56,23,89,23,45,90,110,34,78]
a = [10, 56, 23, 89, 23, 45, 90, 110, 34, 78]
max_no = max(a)
a.remove(max_num)
second_max_no = max(a)
print("The 2nd Largest number in the list is:", second_max_no)

The 2nd Largest number in the list is: 90

Q1. write a python program to multiply an Identity matrix of dimension 3-by-3


a=[[ 9 10 11] [12 13 14] [15 16 17]]
import numpy as np
identity_matrix = np.identity(3)
a = np.array([[23, 13, 19],
[76, 67, 39],
[85, 27, 21]])
result = np.dot(identity_matrix, a)
print("Resultant matrix:")
print(result)

Resultant matrix:
[[23. 13. 19.]
[76. 67. 39.]
[85. 27. 21.]]

Q5. Write a python program to add matrices of dimension 3-by-3 with given matrices.
a=[[ 9 10 11] [12 13 14] [15 16 17] b=[[ 19 30 11] [12 13 44] [15 56 17]]
import numpy as np
a = np.array([[9, 10, 11],
[12, 13, 14],
[15, 16, 17]])

b = np.array([[19, 30, 11],


[12, 13, 44],
[15, 56, 17]])

result = np.add(a, b)
print("Resultant matrix:")
print(result)

Resultant matrix:
[[28 40 22]
[24 26 58]
[30 72 34]]

Q1. Write a python Matplotlib program to plot graph between x & y coordinates.
x = [1, 2, 3, 4, 5, 6, 7, 8] y = [2, 3, 1, 3, 1, 4, 2, 3]
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5, 6, 7, 8]
y = [2, 3, 1, 3, 1, 4, 2, 3]
plt.plot(x, y)

plt.title("Graph of x and y coordinates")


plt.xlabel("x")
plt.ylabel("y")
plt.show()
Q1.In Linux environment, create a python file for addition of two lists. Run
codefile---addtwolist.py
Screen Shot:

You might also like