Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 142 - CodePilot V5.0 C7
P. 142

    step: This optional value defines how much the value should increase or decrease each time.
                         The default step value is 1, which means the numbers go up one at a time. You can also use a
                         negative step value to create a descending sequence.

                         For example: range(10, 2, -2) gives [10, 8, 6, 4].
                                        range(2, 8, 3) gives [2, 5].

                      FOR LOOP IN PYTHON

                      The  for loop repeats a block  of statements a  fixed                     Start
                      number of times. It is commonly used to go through
                      (iterate over) a sequence like a list, a string or a range
                      of numbers. It can be specified using a sequence or a         False   Loop condition
                      range of values.
                                                                                                               increment/
                      Syntax of the for loop:                                                       True       decrement

                      for variable in sequence:                                          Code to be executed

                          Statement block to be repeated
                      The for loop variable iterates over the set of values given               Stop
                      in the sequence, repeating the loop body once for each
                      item. Therefore, the loop header itself defines how many times the loop is going to repeat. For
                      each iteration, the loop variable takes up the next value from the set and executes the loop body

                      one more time. The process goes on till the last item of the set has been used.

                        Program 8 To print each character of a string.

                          Program8.py                                                                Output
                       File  Edit  Format   Run    Options   Window    Help                       H

                       for i in "Hello":                                                          e
                           print(i)                                                               l
                                                                                                  l
                                                                                                  o

                      Syntax of the for loop with the range():

                      for variable in range(start, stop, step):

                          Statement block to be repeated
                      The for loop initialises the variable with the start value. With each iteration, the variable
                      changes based on the step value—increment or decrement. This continues until it reaches the
                      stop value (which is not included). The code inside the loop runs once for each value. This makes
                      it easy to repeat tasks in a clear and controlled way when you know how many times you want
                      to repeat them.

                      140
                            CodePilot (V5.0)-VII

| 137 138 139 140 141 142 143 144 145 146 147 |