Page 143 - Information_Practice_Fliipbook_Class11
P. 143

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



            6.3 for Statement

            Now we are ready to illustrate how we can use Python's for statement to shorten Program 6.1 and its extensions to
            compute the average marks of a large number of students (see Program 6.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  129
   138   139   140   141   142   143   144   145   146   147   148