Page 157 - Information_Practice_Fliipbook_Class11
P. 157

6.6.1 Understanding Nested Loops

            Let us examine the following code snippet:
              01 for i in range(1,3):
              02     print('In the outer loop, i:', i)
              03     for j in range(1,4):
              04         print('    In the Inner loop (i,j)', (i,j))
            In the above code snippet, there are two loops involving the for statement. We call the first loop the outer loop and
            the second loop the inner loop. Accordingly, the first for statement is referred to as the outer for statement, and the
            second for statement is referred to as the inner for statement. The control variable i in the outer for loop takes
            values in the range(1,3), i.e., 1 and 2. For each value of i, the following two statements form the body of the for
            loop. As the second statement in the body is itself a for statement, its body is executed for each value of the control
            variable j in range(1,4), i.e., 1, 2, 3. Thus, we get the following output:

            In the outer loop, i: 1
                In the Inner loop (i,j) (1, 1)

                In the Inner loop (i,j) (1, 2)
                In the Inner loop (i,j) (1, 3)
            In the outer loop, i: 2
                In the Inner loop (i,j) (2, 1)
                In the Inner loop (i,j) (2, 2)

                In the Inner loop (i,j) (2, 3)
            6.6.2 Multiplication Tables

            Students  in  elementary  classes  are  often  required  to  memorise  the  multiplication  tables.  Suppose  you  need  to
            help  your  sister  with  memorising  the  multiplication  tables.  To  help  her,  you  need  to  know  up  to  which  number
            (upperLimit) she needs the multiplication tables. For example, in the following table, the upper limit is 15.
              1   2   3   4   5   6   7   8   9   10   11   12   13   14   15
              2   4   6   8  10  12  14  16  18   20   22   24   26   28   30
              3   6   9  12  15  18  21  24  27   30   33   36   39   42   45
              4   8  12  16  20  24  28  32  36   40   44   48   52   56   60
              5  10  15  20  25  30  35  40  45   50   55   60   65   70   75
              6  12  18  24  30  36  42  48  54   60   66   72   78   84   90
              7  14  21  28  35  42  49  56  63   70   77   84   91   98  105
              8  16  24  32  40  48  56  64  72   80   88   96  104  112  120
              9  18  27  36  45  54  63  72  81   90   99  108  117  126  135
             10  20  30  40  50  60  70  80  90  100  110  120  130  140  150
            Now  let  us develop a program  that takes  upperLimit  as  an  input  and  prints  the  multiplication  tables.  The
            multiplication table has 10 rows. The ith row has an ith multiple of each number in the range range (upperLimit+1).
            So, the outer loop would run for each row, and the inner loop would run for column (col), and print i*col (see Program
            6.10). To make the table nice looking, we print each number using three column positions and use the specification
            '{0:3}'.format(i*col) for printing. The statement print('{0:3}'.format(i * col), end=' ')
            prints the product of i and col formatted to take up at least 3 characters (including digits and spaces). The end=' '
            parameter ensures that the values are separated by a space rather than a newline character. The numbers in each row
            are separated by a blank. Once a row is complete, we transfer the print control to the next line by executing a print
            statement (line 7). For details, please see Program 6.10.






                                                                                              Looping in Python  143
   152   153   154   155   156   157   158   159   160   161   162