NCERT Solutions Class 11, Computer Science, Chapter- 9, Lists
Exercise
1. What will be the output of the following statements?
(i) list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)
Solution:
The sort() method will sort the list in ascending order in place.
OUTPUT:
[10, 12, 26, 32, 65, 80]
(ii) list1 = [12,32,65,26,80,10]
sorted(list1)
print(list1)
Solution:
The sorted() function takes a list as a parameter and creates a new list consisting of the same elements arranged in sorted order. It doesn’t change the list which is passed as a parameter itself.
OUTPUT:
[12, 32, 65, 26, 80, 10]
(iii) list1 = [1,2,3,4,5,6,7,8,9,10]
list1[::-2]
list1[:3] + list1[3:]
Solution:
The statement in line 2 has a negative step size. This means that the list will be traversed in reversed order with step size 2.
OUTPUT: [10, 8, 6, 4, 2]
The statement in line 3 will print the complete list. i.e. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
(iv) list1 = [1,2,3,4,5]
list1[len(list1)-1]
Solution:
This will print the last element of the list. i.e. 5
list1[len(list1)-1]
list1[5-1]
list1[4]
5
2. Consider the following list myList. What will be the elements of myList after the following two operations:
myList = [10,20,30,40]
(i) myList.append([50,60])
Solution:
append() function takes only one argument and inserts the element to the end of the list. Here, the argument is a list containing the elements 50, and 60. Therefore, the output will be: [10, 20, 30, 40, [50, 60]].
(ii) myList.extend([80,90])
Solution:
extend() function takes a list as an argument and appends each element of the list in the same order. The final output will be: [10, 20, 30, 40, [50, 60], 80, 90].
3. What will be the output of the following code segment:
myList = [1,2,3,4,5,6,7,8,9,10]
for i in range(0,len(myList)):
if i%2 == 0:
print(myList[i])
Solution:
Here, the elements of the list will be printed which are at index such that index%2==0. The index of the list can be represented as:
Element 1 2 3 4 5 6 7 8 9 10
Index 0 1 2 3 4 5 6 7 8 9
Therefore, the output will be the elements at index 0, 2, 4, 6, 8 i.e.
1
3
5
7
9
4. What will be the output of the following code segment:
(a) myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList)
Solution:
The statement in line 2 will delete the elements from index 3 to the last index of the list. Therefore, the output will be [1, 2, 3].
(b) myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)
Solution:
The statement in line 2 will delete the elements from start (index 0) to index 4 of the list. Therefore, the output will be [6, 7, 8, 9, 10].
(c) myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)
Solution:
The statement in line 2 will delete the elements from the start to the end of the list with step size 2. i.e it will delete elements starting from index 0, then index 2, index 4, index 6 … and so on. The output will be the remaining elements i.e. [2, 4, 6, 8, 10].
5. Differentiate between append() and extend() functions of list.
Solution:
The append() function takes a single element as an argument and appends it to the list. The argument can be any datatype, even a list. The argument will be added as a single element.
>>>list1 = [10, 20, 30]
>>>list1.append(40)
list1 will now be [10, 20, 30, 40].
>>>list1 = [10, 20, 30]
>>>list1.append([40, 50])
Here [40, 50] is a list that will be considered as a single argument, and will be added to the list at index 3. 'list1' will now be [10, 20, 30, [40, 50]].
The extend() function takes any iterable data type (e.g. list, tuple, string, dictionary) as an argument and adds all the elements of the list passed as an argument to the end of the given list. This function can be used to add more than one element in the list in a single statement.
>>> list1 = [2, 4, 5, 6]
>>> list1.extend((2, 3, 4, 5))
>>> print(list1)
OUTPUT:
[2, 4, 5, 6, 2, 3, 4, 5]
6. Consider a list:
list1 = [6,7,8,9]
What is the difference between the following operations on list1:
(a) list1 * 2
Solution:
The statement will print the elements of the list twice, i.e [6, 7, 8, 9, 6, 7, 8, 9]. However, the list1 will not be altered.
(b) list1 *= 2
Solution:
This statement will change the list1 and assign the list with repeated elements i.e [6, 7, 8, 9, 6, 7, 8, 9] to list1.
(c) list1 = list1 * 2
Solution:
This statement will also have the same result as the statement 'list1 *= 2'. The list with repeated elements i.e [6, 7, 8, 9, 6, 7, 8, 9] will be assigned to list1.
7. The record of a student (Name, Roll No., Marks in five subjects and percentage of marks) is stored in the following list:
stRecord = ['Raman','A-36',[56,98,99,72,69], 78.8]
Write Python statements to retrieve the following information from the list stRecord.
a) Percentage of the student
b) Marks in the fifth subject
c) Maximum marks of the student
d) Roll no. of the student
e) Change the name of the student from ‘Raman’ to ‘Raghav'.
Solution:
List Name |
Name |
Roll no. |
Marks in 5 subjects |
Percentage |
stRecord |
Raman |
A-36 |
[56, 98, 99, 72, 69] |
78.8 |
Index |
0 |
1 |
2
[0,1,2,3,4]
index of the list at index 2 |
3 |
Here, we can see that the 'name' is stored at index 0, 'roll no' at index 1, 'marks of the 5 subjects' is stored at index 2 in a list that contains 5 elements, and 'percentage' at index 3.
(a) Percentage: stRecord[3]
(b) Marks in 5th Subject: stRecord[2][4]
(c) Maximum Marks of the Student: max(stRecord[2])
(d) Roll no. of the student: stRecord[1]
(e) Change the name from Raman to Raghav: stRecord[0] = "Raghav".