Page 285 - Ai_C10_Flipbook
P. 285

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:


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

                        1
                        2
                        3
                        4
                        5

                 [2]:   for Friends in ["Anu", "Ritu", "All", "Sonia"]:
                            print(Friends)

                        Anu
                        Ritu
                        All
                        Sonia

                 [3]:   for Vowels in "AEIOU":
                            print(Vowels)

                        A
                        E
                        I
                        O
                        U

                 Using range() Function
                 The range( ) function is an inbuilt function that is used to generate a sequence of values between the specified
                 range. The syntax to use the for loop with a range() function is:

                     for <Var> in range(<Start>,<End>,<Step>):

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

                    • Start is a starting value of loop, End is an ending value (not inclusive) of loop, and Step is the number by which
                   counter variable will be incremented or decremented. By default step value is 1.
                    • If only one parameter is used the start becomes 0 and step becomes 1 as default.
                    • If Start > End then Step = is a negative integer.

                    • If Start < End then Step = is a positive integer.
                    • If  Start  >=  End  and  Step  value  is  not  specified,  the  loop  will  not  execute  as  this  is  an  invalid  condition.





                                                                                    Advance Python (Practical)  283
   280   281   282   283   284   285   286   287   288   289   290