Page 209 - Computer Science Class 11 Without Functions
P. 209
• A suite (lines 12-15), comprising the following two statements:
marks = int(input('Enter marks:')) #S1
sumMarks = sumMarks + marks #S2
Invoking the function range() with the argument nStudents yields a sequence of numbers 0, 1, 2,...
nStudents-1 (i.e., 0, 1, 2,.., 5) because we have entered 6 as the value of the variable nStudents. Thus, the
for statement serves as a means to count six numbers from 0 up to 5. Indeed, the variable count takes values in the
range(0, nStudents) one by one. For each value of the variable count, each statement in the suite (following
the header) is executed. Initially, the count is 0. So, for count = 0, lines 12-15 are executed in sequence. Since lines
12 and 13 are just comments, the Python interpreter doesn't care about them. So, only lines 14 and 15 are executed
by the Python interpreter. On execution of line 14, the program reads a student's marks. On execution of line 15, the
marks that have just been read get added to sumMarks (initialised as 0). Next, the Python interpreter checks whether
the end of the sequence has already been reached. If so, the execution of the for statement comes to an end and the
control moves to the statement following the for statement, or comes to an end if there are no statements following
the for statement. As the current value of variable count is 0, the next element in the sequence (let us denote it
by next(count)) is 1. So, for the updated value of count (=1), lines 14 and 15 are again executed in sequence, and
marks obtained by the next student are read and added to sumMarks. Next, the Python interpreter checks whether
the end of the sequence has already been reached. Continuing in this manner, when count reaches 5, lines 14 and
15 are again executed in order, and the next student's marks are read and added to sumMarks. Next, the Python
interpreter checks one more time whether the end of the sequence has already been reached. Indeed, there are no
more elements in the range(0, nStudents). So, the execution of for statement comes to an end and the control
moves to line 16. The execution of line 16 computes the average marks. Finally, the average marks are displayed on
execution of line 17, and the program comes to an end.
Program 9.2 Efficient computation of average marks of a group of students in an examination
01 '''
02 objective: To find the average marks of a group of students
03 in an examination.
04 Inputs: To be entered interactively
05 nStudents: Number of studentsAccept data interactively
06 marks: marks of students to be entered one ny one
07 Output: Average marks
08 '''
09 sumMarks = 0
10 nStudents = int(input('Enter number of students: '))
11 for count in range(0, nStudents):
12 #range(nStudents) includes 0, 1, ..., nStudents-1
13 #Read and add to sumMarks a student's marks
14 marks = int(input('Enter marks: ')) #S1
15 sumMarks = sumMarks + marks #S2
16 averageMarks = sumMarks/nStudents
17 print('average marks:', averageMarks)
>>> Enter number of students: 6
>>> Enter marks: 60
>>> Enter marks: 70
>>> Enter marks: 55
>>> Enter marks: 75
>>> Enter marks: 85
>>> Enter marks: 45
average marks: 65.0
Looping in Python 207

