Page 237 - Computer Science Class 11 With Functions
P. 237
10 LOOPING IN PYTHON
Chapter Outline
10.1 Need for Repeated Execution of Statements 10.2 range()
10.3 for Statement 10.4 while statement
10.5 Jump Statements 10.6 Nested Loops
10.7 Printing Patterns 10.8 Infinite Loop
10.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.
10.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 10.1
Program 10.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 in an examination.
03 Inputs: Accept data interactively
04 Output: Average marks
05 '''
06 sumMarks = 0
07 nStudents = 10
08 marks = int(input('Enter marks: '))
09 sumMarks = sumMarks + marks
10 marks = int(input('Enter marks: '))
11 sumMarks = sumMarks + marks
12 marks = int(input('Enter marks: '))
13 sumMarks = sumMarks + marks
Looping in Python 235

