Page 45 - 2501_KVS_C-8
P. 45

3.3 USE OF "RANGE()" FUNCTION
                 Python supports a variety of functions which make it very rich in doing the complex things
                 very easily. One  such function is “range()”.

                 “range()” function is used to generate a sequence of set of values starting from a particular
                 number called lower limit to another number called upper limit. A sequence is the occurrence
                 of values in a linear order. It works on integer or numeric values only.
                 Let us now see how the range() function works. Its syntax is:

                               range(<lower-limit>, <upper-limit>)

                 The range(1,n) function will generate a sequence of values from 1 to n-1 with an increasing
                 value(called Step-value) of 1 by default.
                 Some few examples are:


                   Statement      Sequence of values generated             Step Value or Increasing Value
                  range(0, 5)                [0,1,2,3,4]                                     1
                  range(10,17)         [10,11,12,13,14,15,16]                                1

                   range(4,9)                [4,5,6,7,8]                                     1
                                                                        1 (as the lower limit is greater than the
                   range(8,0)                 Blank [ ]
                                                                                       upper limit)


                 APPLICATION OF “RANGE()” FUNCTION WITH SKIP VALUES
                 As we have discussed above, “range()” function is used to generate a sequence of values
                 starting from a particular number called lower limit to another number called upper limit
                 with a DEFAULT SKIP VALUE of 1. Here, Skip value means the next value which comes

                 after one value, then 2  value, and so on.
                                          nd
                 But  in  this section, we shall  discuss that this default skip value may  be changed  and
                 therefore, its output result. We may pass a Skip value in the range() function that will skip
                 that much value in the previous value and generate a new value till the upper-limit.

                 In the statement range(1,n,2), one will get a sequence of values (1, 3, 5, … up to n - 1).

                 We can better understand this by using the following example:
                               Statement          Sequence of values generated          Step/Skip Value

                              range(0, 5, 2)                    [0,2,4]                          2
                             range(10,17, 3)                  [10,13,16]                         3
                              range(4,9, 2)                     [4,6,8]                          2
                              range(8,0, -1)               [8,7,6,5,4,3,2,1]                    -1
                              range(8,0, -2)                   [8,6,4,2]                        -2

                              range(8,0, -3)                    [8,5,2]                         -3

                 The main application of the range() function will come along with ‘for’ loop which will be
                 discussed in the next section with plenty of examples.


                                                                     Logical Operators in Python                    43
   40   41   42   43   44   45   46   47   48   49   50