Page 70 - Code & Click - 8
P. 70

Experiential Learning
              Lab Activity 7
             To accept a number from the user and display its factorial using the While loop.
             (The factorial of a positive number is the product of all numbers from 1 to the number.  For
             example, the factorial of 5 is 1 × 2 × 3 × 4 × 5 = 120)
             1. Type the code as shown in the image in the Python Editor.
















             2. Press the F5 key to execute the code. Sample outputs of the program are given below.













            JUMP STATEMENTS IN A LOOP IN PYTHON
            Sometimes, we may need to end a loop prematurely, earlier than the logical termination of the loop.
            We may also need to execute the loop from the start without executing a few statements. Python
            provides two statements – Break and Continue, for this purpose. These are called Jump statements
            as they let the execution control jump to a specific position in the program.

            The Break Statement
            The Break statement is used to terminate a loop and transfer the execution control to the statement
            outside the body of the loop.
                                                                                                 Experiential Learning
              Lab Activity 8
             To display all numbers from 1 to 10 using the For loop but to terminate the loop as soon as a
             multiple of 7 is encountered.
             1. Type the code as shown in the image in the Python Editor.














             2. Press the F5 key to execute the code. Sample output of the program is given.



            68
   65   66   67   68   69   70   71   72   73   74   75