Page 112 - modular4.0
P. 112

Chapter Profile


                                    "Imagine, you possess the power of jump statement, allowing you to leap to any
                                    part of the universe. Where would  you jump to and what quests or missions

                                    would you undertake in each dimension?"



                   Program 10: To add first five natural numbers.


                      Program10.py

                   File  Edit  Format   Run    Options   Window    Help

                   #Program to add first five natural numbers

                   sum = 0
                   a = 1
                   while (a <= 5):
                       sum += a                                                                                                             Exercise
                       a += 1
                   print('The sum of first five natural number is', sum)
                                                                                                                                            A.   Tick ( ) the correct option.
                                                                                                                                                 1.  Which of the following statement executes for a fixed number of times?

                                                                                                                                                     a.  break                                         b.  continue
                  You will get the following output:
                                                                                                                                                     c.  while                                         d.  for
                      Output
                                                                                                                                                 2.  What does range(4,8) is equivalent to?
                   The sum of first five natural number is 15
                                                                                                                                                     a.  4,5,6,7,8                                     b.  0,1,2,3,4
                                                                                                                                                     c.  4,5,6,7                                       d.  0,1,2,3,4,5,6,7
                                                                                                                                                 3.  Choose the default value of step size in range() function.
                   Program 11: To print the pattern of stars in decreasing order in the form of triangle.
                                                                                                                                                     a.  0                                             b.  1
                  **********
                  *********                                                                                                                          c.  2                                             d.  -1
                  ********                                                                                                                       4.  What does the continue statement do in a loop?
                  *******
                  ******                                                                                                                             a.  Exit the loop                                 b.  Skip to the next iteration
                  *****
                  ****                                                                                                                               c.  Stop the program                              d.  Restart the program
                  ***                                                            Output
                  **                                                                                                                        B.   Fill in the blanks using the words given below:
                                                                              **********
                  *
                                                                              *********
                      Program11.py                                            ********                                                                                        Sequence, break, infinite loop, in-built
                                                                              *******
                   File  Edit  Format    Run   Options   Window    Help       ******                                                             1.                     is a keyword in Python which is used for sending the program control out of the
                                                                              *****
                   #Program to create star pattern                                                                                                   loop.
                                                                              ****
                                                                              ***                                                                2.  Range() function is an                  function of Python.
                   for i in range(10, 0, -1):
                                                                              **
                       print("*"*i)                                           *                                                                  3.  A                    is a succession of values bound together by a single name.
                                                                                                                                                 4.                     loop never ends.





                110  Modular (Ver. 4.0)-VIII
   107   108   109   110   111   112   113   114   115   116   117