You are on page 1of 44
True/False Questions Question 1 The list() and copy() are the similar functions. False Question 2 The pop( ) and remove( ) are similar functions. False Question 3 A =[] and A = list() will produce the same result. True Question 4 Lists once created cannot be changed. False Question 5 To sort a list, sort() and sorted(), both can be used. True Question 6 The extend() adds a single element to a list. False Question 7 The append( ) can add an element in the middle of a list. False Question 8 The insert() can add an element in the middle of a list. True Question 9 The del statement can only delete list slices and not single elements from a list. False Question 10 The del statement can work similar to the pop( ) function. True Type A : Short Answer Questions/Conceptual Questions Question 1 Discuss the utility and significance of Lists in Python, briefly. Answer Python lists are containers that can store an ordered list of values of same or different data types together in a single variable. The fact that elements of a list need not be homogeneous makes them highly adaptable and powerful data structure in Python. Lists provide fast access to its elements using index numbers. Python lists are mutable which makes them memory efficient. They serve as the basic building blocks for programs that process large amounts of data. Question 2 What do you understand by mutability? What does "in place" memory updation mean? Answer Mutability means that the value of an object can be updated by directly changing the contents of the memory location where the object is stored. There is no need to create another copy of the object in anew memory location with the updated values. This updation of the existing memory location of the object is called as in place memory updation. Question 3 Start with the list [8, 9, 10]. Do the following using list functions: e Set the second entry (index 1) to 17 e Add 4, 5 and 6 to the end of the list e Remove the first entry from the list ¢ Sort the list e Double the list e Insert 25 at index 3 Answer listA = [8, 9, 10] e listA[1] = 17 e listA.extend([4, 5, 6]) e listA.pop(0) e listA.sort() e listA =listA * 2 e listA.insert(3, 25) Question 4 If ais [1, 2, 3] e what is the difference (if any) between a* 3 and [a, a, a]? e isa*3 equivalent toat+ta+a? e what is the meaning of al[1:1] = 9? e what's the difference between a[1:2] = 4 and a[1:1] = 4? Answer fe aS — all 2) oll 23] [a, a, a] > [[1, 2, 3], [1, 2, 3], [1, 2, 3] So, a * 3 repeats the elements of the list whereas [a, a, a] creates nested list. e Yes, botha* 3 anda+a+a will result in [1, 2, 3, 1, 2, 3, 1, 2, 3] e al[1:1] = 9 will cause an error because when list is modified using slices, the value being assigned must be a sequence but 9 is an integer not a sequence. Both a[1:2] = 4 and a[1:1] = 4 will cause error because when list is modified using slices, the value being assigned must be a sequence but 4 is an integer not a sequence. Assuming the question was a[1:2] = [4] and a[1:1] = [4], a[1:2] = [4] will change element at index 1 to 4 as a[1:2] gives a slice with a[1] as its only element. Thus, a becomes [1, 4, 3]. Coming to a[1:1] = [4], a[1:1] returns an empty slice so 4 is inserted into the list at index 1. Thus, a becomes [1, 4, 2, 3]. Question 5 What's a[1 : 1] if ais a list of at least two elements? And what if the list is shorter? Answer a[x:y] returns a slice of the sequence from index x to y- 1. So, a[1 : 1] will return an empty list irrespective of whether the list has two elements or less as a slice from index 1 to index 0 is an invalid range. Question 6 How are the statements Ist = Ist + 3 and Ist += [3] different, where Ist is a list? Explain. Answer The statement Ist = Ist + 3 will give error as + operator in Python requires that both its operands should be of the same type but here one operand is list and other is integer. The statement Ist += [3] will add 3 at the end of the Ist as += when used with lists requires the operand on the right side to be an iterable and it will add each element of the iterable to the end of the list. Question 7 How are the statements Ist += "xy" and Ist = Ist + "xy" different, where Ist is a list? Explain. Answer The statement Ist = Ist + "xy" will give error as + operator in Python requires that both its operands should be of the same type but here one operand is list and other is string. The statement Ist += "xy" will add 'x' and 'y' at the end of the Ist as += when used with lists requires the operand on the right side to be an iterable and it will add each element of the iterable to the end of the list. Question 8 What's the purpose of the del operator and pop method? Try deleting a slice. Answer The del statement is used to remove an individual element or elements identified by a slice. It can also be used to delete all elements of the list along with the list object. For example, Ist = [1, 2, 3, 4, 5, 6, 7, 8] del Ist[1] # delete element at index 1 del Ist[2:5] # delete elements from index 2 to 4 del Ist # delete complete list pop() method is used to remove a single element from the given position in the list and return it. If no index is specified, pop() removes and returns the last element in the list. For example, Ist = [1, 2, 3, 4, 5, 6, 7, 8] # removes element at # index 1 i.e. 2 from # the list and stores # in variable a a = pop(1) # removes the last element # i.e. 8 from the list and # stores in variable b b = pop() Question 9 What does each of the following expressions evaluate to? Suppose that L is the list ["These’, ["are", "a", "few", "words'], "that", "we", "will", "use"]. e L[1][0::2] e "a" in L[1][0] e L[:1]+L[1] e L[2::2] e L[2][2] in L[1] Answer e [are’, 'few] e True ¢ [These’, ‘are’, ‘a’, ‘few’, 'words)] e [‘that’, 'will’] e True Explanation L[1] returns ["are", "a", "few", "words']. L[1][0::2] returns a slice of ["are’, "a", "few", "words'] starting at index 0 covering every alternate element till the end of the list. So, final output is ['are’, few’). L[1][0] is "are". As "a" is present in "are" so output is True. L[:1] return L[0] i.e. ["These']. L[1] returns ["are", "a", "few", "words']. + operator adds the two in a single list to give the final output as [‘These’, ‘are’, ‘a’, ‘few’, ‘words)). L[2::2] returns a slice of L starting at index 2 covering every alternate element till the end of the list. So, final output is ['that’, ‘will']. e L[1] is ["are", "a", "few", "words". L[2][2] is "a". As "a" is present in L[1] so output is True. Question 10 What are list slices? What for can you use them? Answer List slice is an extracted part of the list containing the requested elements. The list slice is a list in itself. All list operations can be performed on a list slice. List slices are used to copy the required elements to a new list and to modify the required parts of the list. For example, Ist = [1, 2, 3, 4, 5] Ist2 = Ist[1:4] #Ist2 is [2, 3, 4] #Using Slices for list modification Ist[0:2] = [10, 20] #lst becomes [10, 20, 3, 4, 5] Question 11 Does the slice operator always produce a new list? Answer Slice operator copies only the requested elements of the original list into a new list. Question 12 Compare lists with strings. How are they similar and how are they different? Answer The similarity between Lists and Strings in Python is that both are sequences. The differences between them are that firstly, Lists are mutable but Strings are immutable. Secondly, elements of a list can be of different types whereas a String only contains characters that are all of String type. Question 13 What do you understand by true copy of a list? How is it different from shallow copy? Answer True copy of a list means that the elements of the original list are copied to new memory locations and the new list contains references to these new memory locations for each element of the list. Hence, in case of true copy changes made to the original list will not reflect in the copied list and vice versa. Incase of shallow copy of a list, the elements of the original list are not copied to new memory locations. Both the new list and the original list refer to the same memory locations for the elements of the list. Hence, changes made to one of the list reflect in the other list as well. Question 14 An index out of bounds given with a list name causes error, but not with list slices. Why? Answer When we use an index, we are accessing a constituent element of the list. If the index is out of bounds there is no element to return from the given index hence Python throws list index out of range error whereas list slicing always returns a subsequence and empty subsequence is a valid sequence. Thus, when a list is sliced outside the bounds, it still can return empty subsequence and hence Python gives no errors and returns empty subsequence. Question 15 What is the difference between appending a list and extending a list? Answer Appending a listExtending a listFor appending to a list, append() function is used.For extending a list, extend() function is used. The append() function can add a single element to the end of a list. The extend() function can add multiple elements from a list supplied to it as argument.After append(), the length of the list will increase by 1 element only.After extend() the length of the list will increase by the length of the list given as argument to extend() Question 16 Do functions max(), min(), sum() work with all types of lists. Answer No, for max() and min(Q) to work on a list, the list must contain all elements of same type (non-complex type) and for sum() to work, the list must contain the elements which can be added such as numbers. Question 17 What is the difference between sort( ) and sorted( )? Answer sort( )sorted( )It modifies the list it is called on. That is, the sorted list is stored in the same list; a new list is not created. It creates a new list containing a sorted version of the list passed to it as argument. It does not modify the list passed as a parameter.It works on a list and modifies it.It can take any iterable sequence type, such as a list or a tuple etc, and it always returns a sorted list irrespective of the type of sequence passed to it.It does not return anything (no return value). It modifies the list in place. It returns a newly created sorted list. It does not change the passed sequence. Type B: Application Based Questions Question 1 What is the difference between following two expressions, if Ist is given as [1, 3, 5] (i) Ist * 3 and Ist *= 3 (ii) Ist + 3 and Ist += [3] Answer (i) Ist * 3 will give [1, 3, 5, 1, 3, 5, 1, 3, 5] but the original Ist will remains unchanged, it will be [1, 3, 5] only. Ist *= 3 will also give [1, 3, 5, 1, 3, 5, 1, 3, 5] only but it will assign this result back to Ist so Ist will be changed to [1, 3, 5, 1, 3, 5, 1, 3, 5]. (ii) Ist + 3 will cause an error as both operands of + operator should be of same type but here one operand is list and the other integer. Ist += [3] will add 3 to the end of Ist so Ist becomes [1, 3, 5, 3]. Question 2 Given two lists: L1 =["this", ‘is’, ‘a’, ‘List’, L2 = ['this", ["is", "another'], "List"] Which of the following expressions will cause an error and why? ° L1==L2 e L1.upper() ¢ L1[3].upper() e L2.upper() ¢ L2[1].upper() ¢ L2[1][1].upper() Answer e L1.upper() will cause an error as upper() method can be called with Strings not Lists. e L2.upper() will cause an error as upper() method can be called with Strings not Lists. e L2[1].upper() will cause an error as L2[1] is a list — [ "is", "another"] and upper() method cannot be called on Lists. Question 3 From the previous question, give output of expressions that do not result in error. Answer e L1 ==L2 gives output as false because L1 is not equal to L2. e L1[3].upper() gives output as 'LIST' because L1[3] is 'List' and upper() function converts it to uppercase. e L2[1][1].upper() gives output as ‘ANOTHER’ because L2[1] ["is", "another"] and L2[1][1] is "another". upper() function converts it to uppercase. Question 4 Given a list L1 = [3, 4.5, 12, 25.7, [2, 1, 0, 5], 88] e Which list slice will return [12, 25.7, [2, 1, 0, 5]? e Which expression will return [2, 1, 0, 5]? e Which list slice will return [[2, 1, 0, 5]]? e Which list slice will return [4.5, 25.7, 88]? Answer L1[2:5] e L1[4] L1[4:5] [2] Question 5 Given a list L1 = [3, 4.5, 12, 25.7, [2, 1, 0, 5], 88], which function can change the list to: e [3, 4.5, 12, 25.7, 88] e [3, 4.5, 12, 25.7] e [[2, 1,0, 5], 88] Answer e L1.pop(4) e del L1[4:6] e del L1[:4] Question 6 What will the following code result in? L1 =[1, 3, 5, 7, 9] print (L1 == L1.reverse( ) ) print (L1) Answer Output False [9, 7, 5, 3, 1] Explanation L1 is not equal to its reverse so L1 == L1.reverse() gives False but L1.reverse( ) reverses L1 in place so after that statement executes, L1 becomes [9, 7, 5, 3, 1]. Question 7 Predict the output: my_list=['p’, 'r’, ‘o, 'b’, I’, ‘e’, 'm'] my_list[2:3] = [| print(my_list) my_list[2:5] = [] print(my_list) Answer Output liDe Ti Ho}, olf 'e, '‘m] ['p, 1 ' maT Explanation my_list[2:3] = [] removes element at index 2 of my_list so it becomes ['p’, 'r’, 'b’, 'l', 'e’, m|]. my_list[2:5] removes elements at indexes 2, 3, and 4 so now my_list becomes ['p’, 'r', 'm']. Question 8 Predict the output: List1 = [13, 18, 11, 16, 13, 18, 13] print(List1 .index(18)) print(List1.count(18)) List1.append(List1.count(13)) print(List1) Answer Output 1 2[13, 18, 11, 16, 13, 18, 13, 3] Explanation List1.index(18) gives the first index of element 18 in List1 which in this case is 1. List1.count(18) returns how many times 18 appears in List1 which in this case is 2. List1.count(13) returns 3 as 13 appears 3 times in List1. List1.append(List1.count(13)) add this 3 to the end of List1 so it becomes [13, 18, 11, 16, 13, 18, 13, 3]. Question 9 Predict the output: Odd = [1,3,5] print( (Odd +[2, 4, 6])[4] ) print( (Odd +[12, 14, 16])[4] - (Odd +[2, 4, 6]) [4] ) Answer Output 410 Explanation Odd + [2, 4, 6] will return [1, 3, 5, 2, 4, 6]. The element at index 4 of this list is 4 so the first output is 4. (Odd +[12, 14, 16])[4] is 14 and (Odd +[2, 4, 6])[4] is 4. 14-4 = 10 which is the second output. Question 10 Predict the output: a, b,c = [1,2], [1, 2], [1, 2] print(a == b) print (ais b) Answer Output True False Explanation As corresponding elements of list a and b are equal hence a == b returns True. ais b returns False as a and b are two different list objects referencing two different memory locations. Question 11 Predict the output of following two parts. Are the outputs same? Are the outputs different? Why? (a) L1, L2 = [2, 4], [2, 4] L3 =L2L2[1] =5 print(L3) (b) L1,L2 = [2, 4], [2, 4] L3 = list(L2) L2[1] = 5 print(L3) Answer Output of part (a) is: [2, 5] Output of part (b) is: [2, 4] AS we can see, outputs of the two parts are different. The reason is that in part (a), the statement L3 = L2 creates a shallow copy of L2 in L3 i.e. both the variables L2 and L3 point to the same list. Hence, when element at index 1 of L2 is changed to 5, that change is visible in L3 also. On the other hand in part (b), the statement L3 = list(L2) creates a true copy (also called deep copy) of L2 so L3 points to a different list in memory which has the same elements as L2. Now when element at index 1 of L2 is changed to 5, that change is not visible in L3. Question 12 Find the errors: e L1=[1, 11, 21, 31] e L2=L1+2 oS. e Idx =L1.index(45) Answer e Line 2—L2=L1 +2 will result in error as one element of + is a list and other is an integer. In Python, operands of + operator should be of same type. e Line 4 — Idx = L1.index(45) will cause an error as 45 is not present in the list Ee Question 13a Find the errors: L1 =[1, 11, 21, 31] An = L1.remove(41) Answer L1.remove(41) will cause an error as 41 is not present in L1. Question 13b Find the errors: L1 =[1, 11, 21, 31] An = L1.remove(31) print(An + 2) Answer An + 2 will cause an error because remove() function does not return the removed element so An will be empty. Question 14a Find the errors: L1 = [8, 4, 5] L2 = L1 * 3 print(L1 * 3.0) print(L2) Answer The line print(L1 * 3.0) causes an error as Python does not allow multiplying a list with a non-int number and 3.0 is of float type. Question 14b Find the errors: L1 = [3, 3, 8, 1, 3, 0,'1', '0', '2', 'e’, 'w;, 'e’, 'r] print(L1[: -1]) print(L1[-1:-2:-3]) print(L1[-1:-2:-3:-4]) Answer The line print(L1[-1:-2:-3:-4]) causes an error as its syntax is invalid. The correct syntax for slicing a list is L1[start:stop:step]. Question 15 What will be the output of following code? x = ['3', '2','5']y =" while x: y = y + x[-1] x = x[:len(x) - 1] print(y) print(x) print(type(x), type(y)) Answer Output 523 [] Explanation The loop while x will continue executing as long as the length of list x is greater than 0. y is initially an empty string. Inside the loop, we are adding the last element of x to y and after that we are removing the last element of x from x. So, at the end of the loop y becomes 523 and x becomes empty. Type of x and y are list and str respectively. Question 16 Complete the code to create a list of every integer between 0 and 100, inclusive, named nums1 using Python, sorted in increasing order. Answer nums1 = list(range(101)) Question 17 Let nums2 and nums3 be two non-empty lists. Write a Python command that will append the last element of nums3 to the end of numsz2. Answer nums2.append(nums3]-1]) Question 18 Consider the following code and predict the result of the following statements. bieber = ['om’, 'nom’, 'nom] counts = [1, 2, 3] nums = counts nums.append(4) e counts is nums e counts is add((1, 2], [3, 4]) Answer e Output is True as both nums and counts refer to the same list. e This will cause an error as add function is not defined in the above code.

You might also like