Page 136 - Plus_V2.2_C8_Flipbook
P. 136

8                                  Loops in Python
















                      Let’s surf

                          The for Statement                            The While Statement
                          Jump Statements                              Some More Programs




                  Let’s PLug-in                                                                     Subject Enrichment


                    State whether these statements are true or false.

                    1.  Python does not support conditional statements.                             ____________
                    2.  We can use if statement inside other if statement.                          ____________

                    3.  We can check multiple conditions in Python.                                 ____________





                  Sometimes, you need to repeat a task multiple time or you may need to repeat the task either a
                  certain number of times or until a condition is satisfied. In Python, the statements that are used to
                  repeat a set of instructions are called iterative or looping statements. Looping statements are very
                  useful and necessary for developing applications.

                  Python  provides  two looping  statements—for and  while.  The  for statement  is used  to repeat  an
                  instruction or a set of instructions a fixed number of times. Whereas, the while statement is used to
                  repeat a set of instructions until a condition evaluates to true. Let’s discuss these constructs in brief.


                                                                                                             for each
                         THE FOR STATEMENT                                                                   item in
                                                                                                            sequence
                  The for statement executes a simple or compound statement for a fixed
                  number of times. The syntax of the for statement is given below:                    Last item    True
                                                                                                      reached?

                  for <variable> in <iterator>:
                                                                                                           False
                         Statements
                                                                                                     Body of for



                                                                                                              Exit loop

                  134   Premium Edition-VIII
   131   132   133   134   135   136   137   138   139   140   141