Page 243 - Computer Science Class 11 With Functions
P. 243

01 def multTable (n) :
              02     print('Multiplication Table of ', n, ':')
              03     for i in range (1, 11):
              04         print (n, '*', i, '=', n*i)
              05
              06 multTable (6)
            Sample Output:
                 Multiplication Table of  6:
                 6 * 1 = 6
                 6 * 2 = 12
                 6 * 3 = 18
                 6 * 4 = 24
                 6 * 5 = 30
                 6 * 6 = 36
                 6 * 7 = 42
                 6 * 8 = 48
                 6 * 9 = 54
                 6 * 10 = 60


             C T  01

                         Additional Programming Q: Rewrite the function multTable so that each number is printed using two positions.





            10.3.4 General Syntax of for Statement

            More generally, the for statement has the following syntax:
            Syntax:
            for control_variable in sequence / values in range:

                body of for loop
            [else:
               statements]
            In the above syntax description,
              for is the keyword.
              in is a keyword (membership operator)

              sequence may be a list, string, tuple or dictionary.
              control variable is a variable that takes the values in the sequence one by one.
               body of the loop may constitute a single statement or several statements, that will be executed for each value of the
              control variable in the sequence. Statements in the body of the loop are indented at the level of indentation next
              to the level at which the header:
                                       for control_variable in sequence / values in range:
              appears. The first statement that appears at the same level as the header marks the end of the body of the for
              statement.

               else  (optional) is a keyword and the statements in the else  block (optional) will be executed after all the
              iterations of for loop are executed. If the loop terminates normally (without a break statement), the statements in
              the else block are executed.

            Let us now see some examples of the use of the for statement that includes an else part.



                                                                                              Looping in Python  241
   238   239   240   241   242   243   244   245   246   247   248