Page 172 - Information_Practice_Fliipbook_Class11
P. 172

2.  Assertion(A):  else clause is optional in for loop.
              Reasoning(R):   The number of times a for loop with a range clause will execute will depend on the values returned by the
                          range().
         Ans.  1. b  2. b


               Case-based Questions

           1.  Pushpreet has a deep interest in Mathematics and is also inclined towards computing skills. He wants to write a program in
              Python that computes and displays the sum of the following series for an arbitrary n (he will provide the value of n when
              the program is executed):
              sum = 1 + (1 + 2) + (1 + 2 + 3) + (1 + 2 + 3 + 4) +  (1 + 2 + 3 + 4 + …+n)
              Write a program for Pushpreet to complete the given task.

         Ans.  def sumSeries(num):
                  '''
                  Objective: To compute the sum of first n terms of the series
                  Input Parameters: num - numeric value
                  Return Value: total - numeric value
                  '''
                  total = 0
                  for i in range(2,  num+2):
                      term = 0
                      for j in range(1,   i):
                          term += j
                      total = total + term
                  return total

              num = int(input('Enter last number in the series'))
              result = sumSeries(num)
              print('Sum of ',   num,   'terms is:' ,   result)
           2.  Sambhav works in marketing department of an IT firm named Diamond Corporation. For the starting slide of his presentation,
              his manager wants to display the following pattern on the screen. The manager wants the size of the pattern to be generic,
              so that it can be decided later.
                                                            *
                                                          * * *
                                                       * * * * *
                                                     * * * * * * *
                                                       * * * * *
                                                          * * *
                                                            *
              Sambhav has been assigned a task to write a program in Python to display the given pattern. Help him complete the
              task.
         Ans.  def diamond(n):
                  for i in range(n):
                      for j in range(n-i-1):
                          print(' ',  end='')
                      for j in range(2*i + 1):
                          print('*',  end='')


          158  Touchpad Informatics Practices-XI
   167   168   169   170   171   172   173   174   175   176   177