Page 8 - IPP-11
P. 8
(d) What will be the output of the following code shown below: (2)
import numpy as np
a = np.array([[3, 2, 1],[1, 2, 3]])
print(type(a))
print(a.shape)
print(a[0][1])
a[0][1] = 150
print(a)
Ans. "<class 'numpy.ndarray'>"
(2, 3)
2
[[ 3 150 1]
[ 1 2 3]]
QUESTION 5
(a) The output of the following snippet of code is either 1 or 2. State whether this statement is true or
false. (2)
import random
random.randint(1,2)
Ans. The above statement is true. The function random.randint(a,b) helps us to generate an integer between
‘a’ and ‘b’, including ‘a’ and ‘b’. In this case, since there are no integers between 1 and 2, the output
will necessarily be either 1 or 2.
(b) What will be the output of the following code for generating NumPy arrays using zeroes: (3)
(i) np.zeros(10)
(ii) np.zeros((3, 6))
(iii) np.zeros((2, 3, 2))
Ans. (i) [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
(ii) [[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0.]]
(iii) [[[0. 0.]
[0. 0.]
[0. 0.]]
[[0. 0.]
[0. 0.]
[0. 0.]]]
(c) Write a Python program for performing linear search in an inputted list. (3)
Ans. #Linear Search
list1= [10,20,30,40,10,50,10]
num=eval(input("Enter a number : "))
found = False
position = 0
while position < len(list1) and not found:
if list1[position] == num:
found = True
position = position + 1
if found:
print(num, "is present at position", position)
else:
print(num, "is not present")
5