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

1.  What will be the output produced on the execution of the following code snippet:
                        for letter in 'world':
                            if letter == 'l':
                                pass
                                print(letter, end ='')
                            print(letter, end='')
                    2.  Answer the following in one word:
                        a. A statement that stops further iterations of a loop.
                        b. A loop inside the body of another loop.
                        c. A statement that does nothing.
                        d. A statement that starts the next iteration of a loop.




                 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 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.




                                                                                              Looping in Python  261
   258   259   260   261   262   263   264   265   266   267   268