Page 340 - Computer Science Class 11 With Functions
P. 340
The variable i is initialized to 0, which is the index of the first element in the list. The while loop continues to
execute as long as the value of i is less than the length of the list. During each iteration of the loop, the current
element of the list is accessed using the index i and printed using the print() function with end=' ' to separate
the elements with a space instead of a newline. After printing the current element, the variable i is incremented by 1
to move to the next element in the list. The loop continues to execute until the value of i is equal to the length of the
list, at which point the loop terminates.
Each element of the list is printed on the same line with a space character between each element, just like in the
previous example using a for loop. However, this code achieves the same result using a while loop instead of a
for loop.
13.3 Nested Lists
A list that includes lists as its elements is called a nested list. For example, examine the list subjectCodes,each of
whose elements is itself a list.
>>> subjectCodes = [['Sanskrit', 78], ['English', 85], ['Maths', 88], ['Hindi', 90]]
>>> for code in subjectCodes:
... print(code)
Sample Output:
['Sanskrit', 78]
['English', 85]
['Maths', 88]
['Hindi', 90]
>>> for code in subjectCodes:
... print(code [0])
...
Sample Output:
Sanskrit
English
Maths
Hindi
The first for loop iterates over the elements of the list subjectCodes and prints individual elements, each of
which is a list comprising the subject name and the subject code. The second for loop iterates over the elements of
the same list subjectCodes, but prints only the first component of each element (i.e., code[0]).
13.4 Heterogeneous List
The items stored in the list can be of any type, such as numeric, string, boolean, list, or functions. For instance, a list
comprising student details, namely name, roll number, course, contact number, and marks in five subjects may be
specified as follows:
>>> student = ['Rohan', 2304, 'B.Sc. Hons Computer Science', 9899410188, [99, 90, 85, 99, 100]]
>>> for element in student:
... print(element)
...
Sample Output:
Rohan
2304
B.Sc. Hons Computer Science
9899410188
[99, 90, 85, 99, 100]
338 Touchpad Computer Science-XI

