Page 148 - Information_Practice_Fliipbook_Class11
P. 148

Example:

         >>> for i in range(6):
         ...     print(i, end=' ')
         ...     i = 10
         ...     print(i)
         ...
         ...
        Sample Output:

              0 10
              1 10
              2 10
              3 10
              4 10
              5 10
        Note that the range() function returns an immutable sequence of values and that the control variable i takes values
        from this sequence. So, changing the value of the control variable in the body of the for statement does not change
        the flow of execution of the for statement.
        6.3.4 More Examples of for Statement

        In Table 6.2, we give some more examples of the use of the for statement:

                                             Table 6.2: Statements using for loop
                       Statements                       Output                        Explanation

         for ch in 'ABCD':                       A                    The control variable ch takes  each value in the
             print(ch)                           B                    sequence of characters: 'A', 'B', 'C',
                                                 C
                                                                      'D'
                                                 D
         numbers = [9, 0, -4, 23, 17, 56] 0                           The control variable n takes values in the list
         for n in numbers:                       -4                   numbers, one by one. For each value of the
             if n%2 == 0:                        56                   control variable,  n,  from the  list  numbers,
                print(n)                                              print statement is executed if n%2 == 0.
                                                 Job Done
         print('Job Done')                                            The statement print('Job Done') is not
                                                                      part of the body of the loop, so it is executed
                                                                      after the loop terminates.
         for x in range(30, 50, 5):              30                   The control  variable,  x, takes values  in  the
             print(x)                            35                   range of 30 to 50 (excluding 50) in steps of 5.
                                                 40
                                                 45
         for p in range(4):                      0 1 2 3              The control variable p takes values in the
            print(p, end=' ')                                         sequence 0, 1, 2, 3. The default start value is 0.
         for p in range(100, 90, -2):            100 98 96 94 92  As the body of the for loop is executed, the
            print(p, end=' ')                    BYE                  numbers  in  the  range  of  100  to  91  (in  steps
         else:                                                        of -2) are printed. The else part of the for
            print("BYE")                                              loop is executed after all iterations of the loop
                                                                      have been executed.









          134  Touchpad Informatics Practices-XI
   143   144   145   146   147   148   149   150   151   152   153