Page 238 - Computer Science Class 11 With Functions
P. 238
14 marks = int(input('Enter marks: '))
15 sumMarks = sumMarks + marks
16 marks = int(input('Enter marks: '))
17 sumMarks = sumMarks + marks
18 marks = int(input('Enter marks: '))
19 sumMarks = sumMarks + marks
20 marks = int(input('Enter marks: '))
21 sumMarks = sumMarks + marks
22 marks = int(input('Enter marks: '))
23 sumMarks = sumMarks + marks
24 marks = int(input('Enter marks: '))
25 sumMarks = sumMarks + marks
26 marks = int(input('Enter marks: '))
27 sumMarks = sumMarks + marks
28 averageMarks = sumMarks/nStudents
29 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.
10.2 range()
Even though we want to know how to shorten Program 10.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 stop is optional. The default values for start and stop 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.
236 Touchpad Computer Science-XI

