0% found this document useful (0 votes)
4 views2 pages

While Loop in Python

The document provides an introduction to loops in Python, explaining their necessity for executing repeated instructions in programming. It describes the 'while' loop structure, including initialization, condition, and update, and provides examples for practical applications. Additionally, it lists programming exercises for students to practice implementing loops for various scenarios.

Uploaded by

aadyamunjal22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

While Loop in Python

The document provides an introduction to loops in Python, explaining their necessity for executing repeated instructions in programming. It describes the 'while' loop structure, including initialization, condition, and update, and provides examples for practical applications. Additionally, it lists programming exercises for students to practice implementing loops for various scenarios.

Uploaded by

aadyamunjal22
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ARYA VIDYA MANDIR GROUP OF SCHOOLS

STD VIII Computer Applications

Loops

Introduction :
All the programs that we have seen until now execute only for one case or
situation but in reality i.e. in real life situations, we need to execute the same set of
instructions for different cases e.g. calculating student’s result – it is not done for a
single student but for all students of the school. This means we need to repeat the same
set of instructions for all the students. This can be accomplished by use of a loop.

What is a Loop ?
A loop is nothing but repeated execution of a set of statements. A loop can be created
in Python by use of any one among the following two statements:
❖ while statement
❖ for statement

The ‘while’ statement :


The while loop is constructed using the following steps :
1. Initialisation
2. Condition
3. Update

Example 1:

# Python program to print Hello Python three times using while loop

count = 0 #initalisation
while (count < 3): #condition
print("Hello Python")
count = count + 1 #update

Example 2:
#Program to calculate and print the area and perimeter of 5 rectangles

count = 0
while (count < 5):
l=int(input('Enter Length of a Rectangle'))
b=int(input('Enter Breadth of a Rectangle'))
area=l*b
p=2*(l+b)
print("Area =",area)
print("Perimeter",p)
count = count + 1
Initial value is the first value that is assigned to the control variable.
It is the value with which the loop begins.
Page | 1
Condition is the condition which is being tested for being true. As long the specified
condition is true, the statements within the loop are executed. And when the condition
evaluates to false, the statements in the loop are skipped; control is transferred to the
statements outside the loop.

Update refers to changing the values of the index variable by incrementing or


decrementing the value of the control variable.
The difference between for and other loops is that in case of for, the execution is
controlled by the programmer whereas in case of while or do... while, the execution is
dependent on the user i.e. it is user controlled.

Write programs for each of the following :


1. To accept the radius of 10 different circles, one at a time and calculate and print
the area and circumference along with the radius.
2. To accept 15 numbers one at a time and print it along with its square and cube.
3. To accept the marks obtained by 5 students in 6 subjects and calculate and print
the total and percentage of each student.
4. To accept the edge of 4 cubes one at a time and calculate and print the volume
and surface area along with the edge.
5. To accept 10 numbers and print the number and whether it is an even number or
an odd number.
6. To accept the age of 15 people and print whether they are major or minor.
7. To accept 10 numbers and print whether they are positive, negative or zero,
along with the number.
8. To accept 10 numbers and count and print the number of even numbers and odd
numbers present among them.
(Please maintain indentation: spacing while writing the Python program)
count_odd = 0
count_even = 0
n=1
while n<11 :
no=int(input('Enter a number'))
if no % 2==0:
count_even+=1
else:
count_odd+=1
n+=1
print("Number of even numbers :",count_even)
print("Number of odd numbers :",count_odd)

9. To accept the age of 15 people and count and print whether the number of
people who are major and minor.
10. To accept 10 numbers and print a count of the number of positive numbers,
negative numbers and zeros.

Page | 2

You might also like