Page 308 - AI Ver 1.0 Class 9
P. 308

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

                           for <Var> in range(<Start>, <End+1>, <Step>):
                               Statements
           Where,
              ✶ for, in and range are keywords.
              ✶ Start, End and Step are parameters of range() function and will always be integers.
              ✶  Start is a starting value of loop and End is an ending value+1 of loop, Step is the number of steps taken to
              reach the end value.
              ✶ If only two parameters are used then Step value becomes 1 by default.
              ✶  If only one parameter is used the Start becomes 0 and Step becomes 1 by default.
              ✶ If Start > End then Step is a -ve integer should be given.
              ✶ If Start < End then Step is a +ve integer.

              ✶ If Start >= End and Step value is not specified then it assumes as +ve which is an invalid condition to run a
              loop so the loop will not execute at all.

           Different examples of for loop with the range( ) function are:
                                           Commands                                         Output

               for count in range(1,6,1):                                            hello
                                                                                     hello
                   print("hello")
                                                                                     hello
               print("Program ends")
                                                                                     hello
                                                                                     hello
                                                                                     Program ends
               for var in range(1,10,2):                                             1,3,5,7,9

                   print(var,end=",")
               for count in range(1,10):                                             1,2,3,4,5,6,7,8,9
                   print(count,end=",")
               for var in range(10):                                                 0,1,2,3,4,5,6,7,8,9
                   print(var,end=",")
               for var in range(5,0,–1):                                             5,4,3,2,1

                   print(var,end=",")
               for var in range(5,5):                                                No output

                   print(var,end=",")
               for var in range(5,3):                                                No output
                   print(var,end=",")
               #Program to generate alternate numbers for the given  enter starting value 5

               range                                                                 enter ending value 10
               starts=int(input("enter starting value "))                            5,7,9,
               ends=int(input("enter ending value "))

               for var in range(starts, ends+1,2):
                   print(var,end=",")

                  306   Touchpad Artificial Intelligence-IX
   303   304   305   306   307   308   309   310   311   312   313