Page 207 - Computer Science Class 11 Without Functions
P. 207
16 sumMarks = sumMarks + marks
17 marks = int(input('Enter marks: '))
18 sumMarks = sumMarks + marks
19 marks = int(input('Enter marks: '))
20 sumMarks = sumMarks + marks
21 marks = int(input('Enter marks: '))
22 sumMarks = sumMarks + marks
23 marks = int(input('Enter marks: '))
24 sumMarks = sumMarks + marks
25 marks = int(input('Enter marks: '))
26 sumMarks = sumMarks + marks
27 marks = int(input('Enter marks: '))
28 sumMarks = sumMarks + marks
29 averageMarks = sumMarks/nStudents
30 print('average marks:', averageMarks)
So far, so good. But what if we were to find the average of the marks obtained by a group of eleven students. Well, we
will have to modify the program by including two more statements, one for accepting the marks of one more student
from the user and another for adding the marks read to the sum (sumMarks). If we were to compute the average
marks of a students' group of size 100, our program would run into over 200 lines of code. The situation would be even
worse if we were to find the average of the marks obtained by 1000 students. Clearly, this way of writing a program
won't work to solve problems that require a lot of calculations. Thanks to Python's for and while statements, a
piece of code can be run over and over again without having to write the same code repeatedly. Repeated execution
of a statement or a sequence of statements is called looping. Hence, for and while statements are also called for
and while loops. The statement or a sequence of statements being executed in a loop is called the loop's body. An
execution of the loop's body is called an iteration of the loop.
9.2 range()
Even though we want to know how to shorten Program 9.1 (which would get even longer if we calculated the average
marks of a lot of students), let's first learn about the built-in function range(), which will help us when we talk about
the for statement. The function range() returns a sequence of numbers within the specified range. The range()
function may be invoked using any of the following formats:
Syntax
range(stop)
range(start, stop)
range(start, stop, step)
The following syntax summarises the above three formats:
range([start,] stop [, step])
In the above description, the arguments start and step are optional. The default values for start and step are 0
and 1, respectively. When these arguments are not given, the function returns a list of integers starting at 0 and ending
at the integer stop, but not including the integer stop. For example, range(6) returns the sequence of integers: 0,
1, 2, 3, 4, 5 (excluding 6). When the range() function is invoked with two arguments, range(start, stop),
it returns a sequence of integers beginning with start and going up to stop. For example, range(2,6) returns the
sequence of integers: 2, 3, 4, 5. Similarly, range(-2, 6) returns the sequence of integers: -2, -1, 0, 1, 2, 3, 4, 5. When
the third argument is also specified, the function call range(start, stop, step) returns a sequence of integers
beginning with start and going up to stop, but stepping over in chunks of size step. For example, the function call
range(-7, 20, 4) returns the sequence of integers: -7, -3, 1, 5, 9, 13, 17. Similarly, the function call range(30,
-10, -8) returns the sequence of integers: 30, 22, 14, 6, -2. Note that the function call range(30, 10) returns an
empty sequence as we cannot count up from 30 to 10.
Looping in Python 205

