Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 118 - 2611_SmartGPT Pro V(5.0) C-7
P. 118

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

                          Statement block to be repeated
                      The  for loop  variable iterates  over  the set  of values                Stop
                      given 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.

                      Program 9. To print the squares of the first five natural numbers.
                          Program9.py                                                                Output

                       File  Edit  Format   Run    Options   Window    Help                       1
                       for i in range(1,6):                                                       4
                                                                                                  9
                            print(i*i)
                                                                                                  16
                                                                                                  25

                       116  Computer Science (V5.0)-VII

| 113 114 115 116 117 118 119 120 121 122 123 |