Page 239 - Computer Science Class 11 With Functions
P. 239
Note that the function call range(30, 10) returns an empty sequence as we cannot count up from 30 to 10.
Similarly, the function call range(10, 30, -2) returns an empty sequence because we cannot count down from
10 to 30 (in steps of -2).
Table 10.1: Use of range()
Function call Sequence Explanation
range(2, 10, 1) [2, 3, 4, 5, 6, 7, 8, 9] start value is 2
last value is 9 (=10-1)
step value is +1
range(-3, 3) [-3, -2, -1, 0, 1, 2] start value is -3
last value is 2 (=3-1)
step value is +1 (default)
range(10, 20, 2) [10, 12, 14, 16, 18] start value is 10
last value is 18 (last in sequence before 20)
step value is +2
range(20, 14, -1) [20, 19, 18, 17, 16, start value is 20
15] last value is 15 (last in sequence before 14)
step value is -1
range(20, 10) [] start value is 20
Unable to count from 10 up to 10 (default step value
is +1)
range(7) [0, 1, 2, 3, 4, 5, 6] start value is 0 (default)
last value is 6 (=7-1),
step up is +1(default)
range(5, 10, 0) Value error For counting, step value must be a non-zero integer.
range(10, 30, 0.5) Value error For counting, step value must be a non-zero integer.
Write the sequence of values that the following function call will return:
1. range(1,14,3)
2. range(100,85,5)
3. range(100,85,-5)
4. range(5)
5. range(5,8)
10.3 for Statement
Now we are ready to illustrate how we can use Python's for statement to shorten Program 10.1 and its extensions
to compute the average marks of a large number of students (see Program 10.2). This program accepts the number
of students (nStudents) as a variable (line 10). So, there is no need to modify the program when the number of
students changes. Suppose, the user enters the number 6 as the value of nStudents. The for statement in the
program comprises:
A header part (line 11), i.e.,
for count in range(0, nStudents):
Looping in Python 237

