Vision International School of Excellence Class: 11 CHAPTER-: List Manipulation
Vision International School of Excellence Class: 11 CHAPTER-: List Manipulation
Q.3. Start with the list [8,9,10]. Do the following using list funcrion:
(a) Set the second entry (index 1) to 17
(b) add 4,5 and 6 to the end of the list.
(c) remove the first entry
(d) sort the list
(e) Double the list
(f) Insert 25 at index 3
Ans3:-
l=[7,8,9]
(a) l[1]=17 #l becomes[7,17,9]
(b) l.extend9[4,5,6]) #l becomes[7,17,9, 4,5,6]
(c) l.pop(0) #l becomes[17,9, 4,5,6]
(d) l.sort() #l becomes[4,5,6,9,17]
(e) l=2*l #l becomes[4,5,6,9,17, 4,5,6,9,17]
(f) l[3]=25 #l becomes[4,5,6, 13,17, 4,5,6,9,17]
Q.5: What’s a [1:1] if a is a string of at least two character? And what if string is
shorter?
Ans:-a [l:r] will output the characters from index l to r-1.
So, if a is a string, a[1:1] will be an empty string because l and r are both equal. Therefor
if a string has at least two characters or not, a [1:1] will be “”(empty string).
Q.6. What is the purpose of del operator and pop method? Try deleting a slice.
Ans: Del and pop both are use to remove an element from the list. The difference is that
pop returns the element that is being removed and del does not. E.g
a=[1,2,3]
a.pop(0) will return 1
del can delete list slices e.g.
a=[1,2,3,4,5,6]
del a[0:2]
print(a) #[3,4,5]
Computer Science CH-5, String Manipulation,, Class 11 (By: SAROJ MEHTA) | Page 1 of 2
Q.7. What does each of the following expressions evaluate to ? Suppose that L is the
list.
[“These”,[“are”, “a”, “few”, “words”], “that”, “we” “will” , “use”].
(a) L[1][0::2]
(b) “a” in L[1][0]
(c) L[:1]+L[1]
(d) L[2::2]
(e) L[2][2] in L[1]
Ans:-
(a). [‘are’, ‘few’]
(b). True
(c). [‘these’, ‘are’, ‘a’, ‘few’, ‘words’]
(d) [‘that’ , ‘will’]
(e). True
Q.8. What are list slices? What for can you use them?
Ans: A list slices in Python refers to a prat of the list which is extracted from another
list. It is a list containing some contagious element of another list. The syntax is :
Listvariable[startingIndex : EndingIndex].
Q.10. Compare lists with strings. How are they similar and how are they different ?
Ans:-The similarity between list and string is that they both are a sequence. But list is a
sequence of elements of the same of different types where as string is a sequence of
characters all of which are string type. List are mutable where as srings are immutable.
So the elements in a list can be changed while the characters in a string can’t be
changed but whole string can be changed.
Q.11. What do you understand by true copy of a list? How is it different from
shallow copy?
Ans:-In case of true copy, changes made in one list do not reflect in the other one. E.g.
a=[1,2,3,4]
b=list(a)
a[0]=5
print(b) # ‘b’ is still [1,2,3,4] even though ‘a’ has becobe[5,2,3,4]
shallow copy is useful for compound objects only, i.e a list containing other lists. A
shallow copy constructs a new compound object and then inserts references into it to
the objects found in the original.
Q.12. An index out of bounds given with a list name causes error, but not with list
slices. Why?
Ans:- Slicing returns a list, if the indices don’t fall within the range of the number of
elements in the list, we can return an empty list, so there is no need to throw an error
but while accessing an element using an index, if the index is out of range, we can’t
return a default value like none(because it could be a valid value in the list), so an error
is thrown.
Hence if a=[1,2,3,4]
a[4] will return an error but a[4:5] won’t
Computer Science CH-5, String Manipulation,, Class 11 (By: SAROJ MEHTA) | Page 2 of 2