Page 143 - TP_Prime_V2.2_Class8
P. 143

Program 1: To print number 1 to 5 using the range().

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



                                                      Using the range( ) Function
                 The range( ) function can also be used without a for loop. It returns a value of True or False.            LOOPS IN PYTHON


                 Program 2: To use the range () without the for loop.
                     Program2.py                                                 Output

                  File  Edit  Format   Run    Options   Window    Help        True

                  x=100
                                                                                                                          141
                  print(x in range (100,1000))





                                                  Using range( ) Function without Loop

                 The preceding example returns True because 100 exists in the range from 100 to 1000.
                 Now, let us try to use the range( ) function in the for loop with increment.

                 Program 3: To use the range() function in the for loop with increament.

                                                                                 Output

                                                                              1
                                                                              6
                                                                              11
                                                                              16
                                                                              21
                                                                              26
                                                                              31
                                                                              36
                                                                              41
                                                                              46
                                                                              51
                                                                              56
                                                                              61
                     Program3.py                                              66
                                                                              71
                  File  Edit  Format   Run    Options   Window    Help        76
                                                                              81
                  for x in range (1,100,5):
                                                                              86
                      print(x)                                                91
                                                                              96




                                                     Using range( ) Function in Loop
   138   139   140   141   142   143   144   145   146   147   148