Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 116 - 2615_KEC
P. 116

    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.

                      Program 6: To print "Hello" 5 times using the range function.

                          Program6.py                                               Output

                       File  Edit  Format   Run   Options  Window    Help         Hello
                                                                                  Hello
                       for i in range (5):
                                                                                  Hello
                           print("Hello")                                         Hello
                                                                                  Hello

                      Program 7: To print odd numbers till 10 using start, stop and step values of a range function.

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

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

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

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

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

                      THE while LOOP

                      The while loop repeats a block of statements while a given condition is true. In this looping construct,
                      first, the condition is checked and if it is true, the control will move inside the loop and execute the

                       114  Premium Edition-VII

| 111 112 113 114 115 116 117 118 119 120 121 |