Page 102 - Code_GPT_Class_8
P. 102

The range( ) function is an in-built function in Python. This function generates a sequence of numbers.
              A sequence is a succession of values bound together by a single name. The range( ) function is generally
              used in the for loop to iterate over the sequence of numbers.

              The general syntax for using the range( ) function is given below:

                   range(start, stop,  step_size):  The  start  specifies  the  starting  point  for  generating  numbers.
                  By default, it starts from 0. The stop specifies the position up to which the numbers are to be
                  generated (last number is not included). The step_size specifies the increment or decrement. By
                  default, the value of the step_size = 1 and numbers are generated with an increment of 1. However,
                  we can generate numbers by specifying the value of step_size according to our requirement.
                  range(start, stop): It generates a set of whole numbers starting from ‘start’ to ‘stop–1’.

                  Example: range(3, 9) is equivalent to [3, 4, 5, 6, 7, 8].

                  range(stop): Generates a set of whole numbers starting from 0 to (stop–1).

                  Example: range(6) is equivalent to [0, 1, 2, 3, 4, 5].

              The start and step_size values are optional in the application of range() function and they can be omitted.
              Pragram 1: To print odd numbers till 10 using start, stop and step values of a range function.


                   Program1.py                                                 Output
                File  Edit  Format   Run   Options   Window    Help         1
                                                                            3
                for i in range (1,10,2):
                                                                            5
                    print(i)
                                                                            7
                                                                            9




              Program 2: To print a selected range of numbers with start and stop values.


                   Program2.py                                                 Output
                File  Edit  Format   Run   Options   Window    Help         3
                                                                            4
                for i in range (3,7):
                                                                            5
                    print(i)
                                                                            6




              Program 3: To print whole numbers from 0 to 9 using only stop value.


                   Program3.py                                                 Output
                File  Edit  Format   Run   Options   Window    Help         0
                                                                            1
                for i in range (5):
                                                                            2
                    print(i)
                                                                            3
                                                                            4





                        CodeGPT (Ver. 4.0)-VIII
                100
   97   98   99   100   101   102   103   104   105   106   107