Page 301 - Computer Science V2.0 Class 11
P. 301

A suite (lines 12–15), comprising the following two statements:

                   marks = int(input('Enter marks:')) #S1
                   sumMarks = sumMarks + marks   #S2
                   Invoking the function range() in line 11 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 iterates six numbers from 0 up to 5. Indeed, the variable count takes values in the range(0,
                   nStudents) one by one. 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 the variable count is 0, the next element in the sequence 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 the 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.
                   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.
                   On execution of line 16, the average marks are computed by dividing the value of sumMarks by the number of
                   students (nStudents)
                  On execution of line 17, the average marks are displayed, and the program comes to an end.

                 Program 11.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 students
                   03 an examination
                   04 Inputs: To be entered interactively
                   05 nStudents: Number of studentsAccept data interactively
                   06 marks: marks of students to be entered one by 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)
                 Sample Output:

                  >>> Enter number of students: 6
                  >>> Enter marks: 60

                                                                                                   Looping in Python  287
   296   297   298   299   300   301   302   303   304   305   306