Page 206 - Computer Science Class 11 Without Functions
P. 206

9                                            LOOPING IN PYTHON














          Chapter Outline


          9.1 Need for Repeated Execution of Statements      9.2 range()
          9.3 for Statement                                  9.4 while Statement
          9.5 Jump Statements                                9.6 Nested Loops
          9.7 Printing Patterns                              9.8 Infinite Loop
          9.9 pass Statement


        Introduction

        In our day-to-day lives, many tasks are performed repeatedly. For example, food is prepared three times a day, plants
        are watered daily, and bills are paid monthly. Similarly, in a program, parts of code are often required to be executed
        repeatedly. For example, we may be required to compute the percentage of marks for each student in a class or compute
        total wages for each employee in the company. Computers can be programmed to execute such tasks repeatedly. The
        repeated execution of statements in a program is called iteration, or loop. Python provides for and while statements
        to perform repeated execution of statements.

        9.1 Need for Repeated Execution of Statements

        Let's say we want to find out what the average marks were for a group of 10 students. From what we've learned so far,
        we know that the task can be easily done by Program 9.1

         Program 9.1 Computation of average marks of a group of 10 students in an examination

          01 '''
          02 objective: To find the average marks of a group of 10 students
          03 in an examination.
          04 Inputs: Accept data interactively
          05 Output: Average marks
          06 '''
          07 sumMarks = 0
          08 nStudents = 10
          09 marks = int(input('Enter marks: '))
          10 sumMarks = sumMarks + marks
          11 marks = int(input('Enter marks: '))
          12 sumMarks = sumMarks + marks
          13 marks = int(input('Enter marks: '))
          14 sumMarks = sumMarks + marks
          15 marks = int(input('Enter marks: '))
         204   Touchpad Computer Science-XI
   201   202   203   204   205   206   207   208   209   210   211