Revision Test - Answer
1. monuments=[ 'Kutub Minar', 10, 'Taj Mahal', 20, 'India Gate', 30, 'Char Minar', ]
states=['Delhi', 'Kerala', 'Tamil Nadu', 'Bihar']
Write Python statements for the following. Kindly state the reason for the operations which not
possible.
a) To insert “Red Fort” and “Jantar Mantar” in the list monuments
b) To add 50 at the end of the list
c) To add list of states at the end of the list of monuments
d) To replace 3rd, 4th and 5th element of the monuments list by “India”.
e) To remove the third element from the list monuments.
f) To delete the list monuments.
g) To store all elements 2 times in the list states.
h) To check whether the element 20 is present in the list or not.
Ans
a)
monuments.extend([“Red Fort” , “Jantar Mantar”])
b)
monuments.append(50)
c)
monuments.extend(states)
d)
monuments[2:5] = ‘India’
e)
monuments.pop(2)
f)
monuments.pop()
g)
states = states * 2
h)
20 in monuments
2. Give the elements of the following tuple which are present at the given index numbers:
planets=('Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune')
a)
planets [5]
b)
planets [2*2]
c)
planets [-4]
d)
planets [::2]
e)
planets [::-2]
f)
planets [-5:-1]
g)
planets [5:2:-2]
h)
planets [-2:-5:-1]
i)
planets=planets+”Tamil”
j)
states=states*2
k)
len(planets)
l)
state.count(‘Delhi’)
Ans
a)
planets [5]
'Saturn'
b)
planets [2*2]
'Jupiter'
c)
planets [-4]
'Jupiter'
d)
planets [::2]
('Mercury', 'Earth', 'Jupiter', 'Uranus')
e)
planets [::-2]
('Neptune', 'Saturn', 'Mars', 'Venus')
f)
planets [-5:-1]
('Mars', 'Jupiter', 'Saturn', 'Uranus')
g)
planets [5:2:-2]
('Saturn', 'Mars')
h)
planets [-2:-5:-1]
('Uranus', 'Saturn', 'Jupiter')
i)
planets=planets+'Tamil'
TypeError
j)
states=states*2
['Delhi', 'Kerala', 'Tamil Nadu', 'Bihar', 'Delhi', 'Kerala', 'Tamil Nadu', 'Bihar']
k)
len(planets)
8
l)
state.count(‘Delhi’)
1
3. Find the output:
l = [e**3 for e in range(10,2,-1)]
[1000, 729, 512, 343, 216, 125, 64, 27]
print(l)
list1=[x+2 for x in range(5)]
[2, 3, 4, 5, 6]
print(list1)
x = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
[6, 24]
result = []
for a,b,c in x:
if (a+b+c) % 2 == 0:
result.append(a+b+c)
print(result)
L=[10,20]
[10, 20, [30, 40], 50, 60]
a)
Predict the output of the following:
L=[10,2,5,-1,90,23,45]
L.sort(reverse=True)
S=L.pop(2)
L.insert(4,89)
L.sort()
L.remove(90)
print(L)
[-1, 2, 5, 10, 45, 89]
4. If we have 2 Python Lists as follows:
L1=[10,20,30]
L2=[40,50,60]
If we want to generate a list L3 as: [10,20,30, 40,50,60]
then best Python command out of the following is:
(a) L3=L1+L2
(c) L3=L1.update(L2)
(b) L3=L1.append(L2)
(d) L3=L1.extend(L2)
5. Identify the correct output of the following Python code:
L=[3,3,2,2,1,1]
L.append(L.pop(L.pop()))
print(L)
(a) [3,2,2,1,3] (b)[3,3,2,2,3] (c)[1,3,2,2,1] (d)[1,1,2,2,3,3]
6. Predict the output of the following:
L1=[1,2,3]
L2=[4,5]
L3=[6,7]
L1.extend(L2)
L1.append(L3)
print(L1)
(a) [1,2,3,4,5,6,7]
(b)[1,2,3,[4,5],6,7]
(c) [1,2,3,4,5,[6,7]]
(d)[1,2,3,[4,5],[6,7]]
7. Suppose a Python tuple T is declared as:
T=('A','B','C')
Which of the following command is invalid?
(a) T=('D')
(b) T=('D',)
(c) T+=('D')
(d) T+=('D',)
8. Write the output of the code given below:
a,b,c,d = (1,2,3,4)
mytuple = (a,b,c,d)*2+(5**2,)
print(len(mytuple)+2)
9. Write the difference between:
A)
pop() and remove()
B)
sort() and sorted()
C)
append() and extend()
D)
list and tuple
10.Write a program to get a list and change the list element by doubling the value. Example: if l =
[4,6,3,9] then the list should change as [16,36,9,81]