Page 208 - Computer Science Class 11 Without Functions
P. 208

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). In Table 9.1, we show more examples of sequences that are returned when the function
        range() is called with different arguments.
                                              Table 9.1: Use of range()function

         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, 15]         start value is 20
                                                                  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 value is +1.
         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.



          C T  01     Write the sequence of values that the following function call will return:

                      range(1,14,3)
                      range(100,85,5)
                      range(100,85,-5)
                      range(5)
                      range(5,8)

        9.3 for Statement

        Now we are ready to illustrate how we can use Python's for statement to shorten Program 9.1 and its extensions to
        compute the average marks of a large number of students (see Program 9.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):

         206   Touchpad Computer Science-XI
   203   204   205   206   207   208   209   210   211   212   213