Page 202 - AI Ver 1.0 Class 10
P. 202

Looping Statements                                                                   For Loop

              The process of repetition of a set of instructions based on a criteria is
              called a loop. There are two types of looping statements in Python, let us
              study about them in detail.
                                                                                                   Condition
                                                                                                  Last item in    True
              The for Loop                                                                        sequence?
              The for loop is used to repeat a set of instructions for a fixed number of
              times. It means when the number of iterations is known/definite before we          False
              start with the execution of a loop. It is therefore also known as a definite
              loop. Indentation of statements is must to specify the block of statements           Body for
              to be repeated using for loop. Let us understand the flow of for loop            Execute statements
              with the help of a flowchart.
              There are commonly two different ways of using for loop.
                                                                                                  Exit for loop
              Using Sequence

                   for <counter variable> in <sequence>:
                          Statements
              Where,
                 • for is a reserved keyword.
                 • counter variable can be any identifier that keeps a count of the loop.

                 • sequence can be any value like integer, string, list etc.
                 • Statements always indented can be single or a block.
              For examples:























              Using range() Function

                  for <Var> in range(<Start>,<End+1>,<Step>):
                  <Statement>
              Where,

                 • for is a reserved keyword.
                 • Start, end, step are parameters of range() function and will always be integers.

                 • Start=starting value of loop. end=ending value+1 of loop, Step=number of steps taken to reach the end
                 value.

                        200   Touchpad Artificial Intelligence-X
   197   198   199   200   201   202   203   204   205   206   207