Page 141 - Information_Practice_Fliipbook_Class11
P. 141

6                                              LOOPING IN PYTHON














              Chapter Outline


              6.1 Need for Repeated Execution of Statements      6.2 range()
              6.3 for Statement                                  6.4 while Statement
              6.5 Jump Statements                                6.6 Nested Loops
              6.7 Printing Patterns                              6.8 Infinite Loop
              6.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.

            6.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 6.1

            Program 6.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: '))

                                                                                              Looping in Python  127
   136   137   138   139   140   141   142   143   144   145   146