Page 141 - TP_Prime_V2.2_Class8
P. 141

09

                                                            LOOPS IN

                                                             PYTHON








                                                                            Learning Objectives                             LOOPS IN PYTHON
                                                                            •  Iterative Statements
                                                                            •  Types of Iterative Statements
                                                                             •  Infinite Loop
                                                                             •  Loop with else Statement




                                 Yesterday my father bought me a toy in which if I press a button 4 times, the            139
                                toy rotates his hands 4 times. Can we do something similar in Python?


                                 Sure we can! Python allows us to repeat a task with the help of iterative
                                statements. Let us learn about them.




                   Iterative Statements


                 The  statements that  repeatedly  execute  a  set  of statements  as  long  as  the  provided
                 condition  is evaluated to true are called  iterative  statements. Each  repetition  of

                 the  statements is called  an iteration.  Iterative  statements are also  known as  looping
                 statements or loops.  To better  understand the looping statements, let us take a

                 real-life example.

                 Suppose  you  run three  rounds of your  playground  everyday,  which  means  you  continue

                 to run in  loop until  three rounds  are completed.  Similarly, Python  programming allows
                 us to create loops that continue to execute as long as the loop condition remains true.

                 Let us understand  how loops work.

                 We  have  a  variable X  that  stores the  number of rounds  you  have  completed.  Initially,
                 X  is set  to 0. After  each  round,  the  value  of X  increases  by  1. Once the  value  of X

                 reaches 3, the condition is met, and the loop stops. In this process, the loop will repeat
                 three times before moving to the next  statement after the loop.
   136   137   138   139   140   141   142   143   144   145   146