Page 164 - Information_Practice_Fliipbook_Class11
P. 164

Let's Summarise


           Ø   The repeated execution of statements in a program is called iteration or looping.
           Ø   Python provides for and while statements to run a piece of code that can be run over and over again.
           Ø    The statement or a sequence of statements being executed in a loop is called the loop's body.
           Ø   range(start, stop, step) returns a sequence of integers beginning with start and going up to stop,
              but stepping over in chunks of size step.
           Ø   Syntax of for statement
              for control_variable in values in range/sequence:
                                   body of for loop
              [else:
                   statements]
              •  A sequence may be a values in range, list, string, tuple, or a dictionary.
              •  Control variable is a variable that takes the values in the sequence one by one.
              •  else block is executed after all the iterations of the for loop are executed.
           Ø   Syntax of while statement:
              while test condition:
                 Body of while loop

              [else:
                 Statements]

              •  test condition is the expression that will evaluate to either True or False
              •   body of while  loop constitutes the statement(s) that will be executed if the test condition is True.
                 These are determined through indentation. The first un-indented line marks the end of the loop.
              •  else (optional) is a keyword and the statements in else block (optional) will be executed after all the
                 possible iterations of the while loop are executed.
           Ø   A while loop becomes an infinite loop if the test condition never yields False.
           Ø   A pass statement is ignored by the Python interpreter.
           Ø   When a continue statement is encountered, the remaining statement(s) in the loop are skipped and the
              control jumps to the beginning of the loop for the next iteration.
           Ø   When the break statement is encountered and executed, no further statement in the loop is executed.



                                               Solved Exercises



        A.  Multiple Choice Questions
           1.  A ____________ construct is used for repeated execution of statements.
              a.  Looping              b. Selection           c. Sequential          d. None of these
           2.   Which of the following statements will terminate a loop?

              a.  pass                 b. continue            c. break               d. All of these
           3.   The expression  range(10, 2, -4) will yield the sequence:
              a.  10, 6, 2             b. 9, 5, 2             c. 10, 6               d. 9, 5
           4.   How many values will be printed on execution of the following loop?

              for z in range(3):
                  print(z)
              a.  4                    b. 2                   c. 3                   d. None of these

          150  Touchpad Informatics Practices-XI
   159   160   161   162   163   164   165   166   167   168   169