Page 434 - CITS - Computer Software Application -TT
P. 434

COMPUTER SOFTWARE APPLICATION - CITS

            Looping


           The following loops are available in Python to fulfil the looping needs. Python offers 3 choices for running the
           loops. The basic functionality of all the techniques is the same, although the syntax and the amount of time
           required for checking the condition differ.
           We can run a single statement or set of statements repeatedly using a loop command.
           The following sorts of loops are available in the Python programming language.


             S. No.   Name of the loop                         Loop Type & Description
                                        Repeats a statement or group of statements while a given condition is TRUE.
               1         While loop
                                        It tests the condition before executing the loop body.
                                        This type of loop executes a code block multiple times and abbreviates the
               2          For loop
                                        code that manages the loop variable.
               3        Nested loops    We can iterate a loop inside another loop.


           Loop Control Statements
           Statements used to control loops and change the course of iteration are called control statements. All the objects
           produced within the local scope of the loop are deleted when execution is completed.

           Python provides the following control statements. We will discuss them later in detail.
           Let us quickly go over the definitions of these loop control statements.

                    Name of the control
             S. No.                                            Loop Type & Description
                         statement
                                        This command terminates  the loop's  execution  and transfers the program's
               1      Break statement
                                        control to the statement next to the loop.
                                        This command skips the current iteration of the loop. The statements following
               2     Continue statement  the continue statement are not executed once the Python interpreter reaches
                                        the continue statement.
                                        The pass statement is used when a statement is syntactically necessary, but
               3       Pass statement
                                        no code is to be executed.

           The for Loop
           Python’s for loop is designed to repeatedly execute a code block while iterating through a list, tuple, dictionary, or
           other iterable objects of Python. The process of traversing a sequence is known as iteration.

           Syntax of the for Loop
           1  For value in sequence:
           2  { code block }
           In this case, the variable value is used to hold the value of every item present in the sequence before the iteration
           begins until this particular iteration is completed.
           Loop iterates until the final item of the sequence are reached.
           Code
           1  # Python program to show how the for loop works

           2
           3  # Creating a sequence which is a tuple of numbers
           4  numbers = [4, 2, 6, 7, 3, 5, 8, 10, 6, 1, 9, 2]
           5
           6  # variable to store the square of the number



                                                           421

 CITS : IT&ITES - Computer Software Application - Lesson 120 - 137  CITS : IT&ITES - Computer Software Application - Lesson 120 - 137
   429   430   431   432   433   434   435   436   437   438   439