Page 132 - ComputerGenius_V2.1_Class8
P. 132

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.
                  Example 6: To print odd numbers till 10 using start, stop and step values of a range function.

                  Program:
                    for i in range (1, 10, 2):
                        print (i)
                  Output:
                    1

                    3
                    5
                    7
                    9

                  Example 7: To print a selected range of numbers with start and stop values.
                  Program:
                    for i in range (3, 7):
                        print (i)
                  Output:
                    3

                    4
                    5
                    6

                  The while Loop

                  The while loop executes a set of statements repeatedly, until the logical expression evaluates to
                  false. When the condition becomes false, the control comes out of the loop. The syntax of while
                  statement is given below:

                  while (test expression):
                         Statements
                         increment/decrement expression

                  Example 8: To print first 5 natural numbers using while loop.
                  Program:
                    i=1

                    while (i<6):
                        print (i)
                        i+=1

                      130    Computer Genius (V2.1)-VIII
   127   128   129   130   131   132   133   134   135   136   137