Page 7 - IPP-11
P. 7
(c) What will be the output of the following: (2)
s = 'PYTHON'
s1= s[::-1]
print(s1)
Ans. NOHTYP
(d) Anurag wants to use the function sqrt() from the module math. Write a statement to help Anurag to
include only the sqrt() function in his program. (2)
Ans. from math import sqrt
QUESTION 4
(a) Give the output of the following NumPy code: (2)
import numpy as np
data = np.array([5,2,7,3,9])
print (data[:])
print(data[1:3])
print(data[:2])
print(data[-2:])
Ans. [5 2 7 3 9]
[2 7]
[5 2]
[3 9]
(b) Write a NumPy code for creating and displaying subset of a given array A below: (4)
If A {1, 3, 5}, then all the possible/proper subsets of A are { }, {1}, {3}, {5}, {1, 3}, {3, 5}
Ans. import numpy as np
def sub_lists(list1):
# store all the sublists
sublist = [[]]
for i in range(len(list1) + 1): # first loop
for j in range(i + 1, len(list1) + 1): # second loop
sub = list1[i:j] # slice the subarray
sublist.append(sub)
return sublist
x = np.array([1, 2, 3, 4]) # driver code
print(sub_lists(x))
(c) Differentiate between a NumPy array and list? (3)
Ans.
NUMPY ARRAY LIST
numpy.Array works on homogeneous Python list is made up for heterogeneous
(same) types. (different) types.
Python list supports adding and removing numpy.Array does not support addition
elements. and removal of elements.
Can’t contain elements of different types Can contain elements of different types
Less memory consumption More memory consumption
Faster runtime execution Runtime execution is comparatively slower
than Arrays.
4