Page 442 - AI Ver 3.0 class 10_Flipbook
P. 442

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.
                 For example:
               [1]:   for Count in range(1,5,1):
                          print("Welcome")
                      print("Iteration Finished")
                      Welcome
                      Welcome
                      Welcome
                      Welcome
                      Iteration Finished

                    440     Touchpad Artificial Intelligence (Ver. 3.0)-X
   437   438   439   440   441   442   443   444   445   446   447