Page 307 - AI Ver 1.0 Class 9
P. 307

Brainy Fact


                True means non-zero number or non-empty object and False means zero number or empty object
                or none.





                    Iterative  Statements


            In Python, the process of repeating a set of instructions based on a condition is called loop. There are two types
            of loops in Python—for loop and while loop. Let us learn about them in detail.


            The for Loop

            The  for  loop  is  used  to  repeat  a  set  of  instructions  for  a  fixed  number  of  times.  It  means  the  number  of
            iterations are known/definite before we start with the execution of a loop. Therefore, the for loop is also known
            as definite loop. Indentation of statements is must to specify the block of statements to be repeated using the
            for loop. There are commonly two different ways of using the for loop:
               • Using Sequence: In this type of for loop, a sequence of values is used on which the loop iterate. The syntax to
              use the for loop with a sequence is:

                       for <counter variable> in <sequence>:
                             Statements
               Where,
                  ✶ for is a keyword.
                  ✶ counter variable is any identifier that keeps a count of the loop.
                  ✶ sequence is any values like integer, string, list, etc.
                  ✶ Statements are always indented can be single or a block.

               Different examples of for loop are:
                                                Commands                                       Output

                    for i in [1,2,3,4]:                                                   1
                        print(i)                                                          2
                                                                                          3

                                                                                          4
                    for words in ["hello", "friends", how are you ?"]:                    hello
                        print(words)                                                      friends
                                                                                          how are you?

                    for alphabets in "hello!":                                            h
                        print(alphabets)                                                  e
                                                                                          l
                                                                                          l

                                                                                          o
                                                                                          !

                                                                                Introduction to Python  305
   302   303   304   305   306   307   308   309   310   311   312