CLASS: HSP-I
SUBJECT: COMPUTER SCIENCE
.
UNIT- 1
CONTROL STRUCTURES IN PYTHON
LOOPS
Iteration statements, also known as loops or
looping statements are used to repeat a set of
statements until a condition is met.
Loop Control Elements
1. Initialization Expression(starting)
Before entering in a loop, its loop variable
must be initialized. The initialization of the loop
variable takes place under initialization
expression(s).
2. Test Expression:
The test expression is an expression whose
truth value decides whether the loop body
gets executed or not. if test expression is true,
the body of loop gets executed ,otherwise the
loop is terminated.
3. Body of the loop
Rohit Pandita Sr. Lecturer Computer Science HSS Purkhoo
The statements that are executed repeatedly form
body of loop.
In awhile loop before every iteration the test
expression is evaluated and if it is true ,the body of
the loop is executed and if test expression is false,
the body of loop is not executed
4. Update Expression:
The Update expression changes the value of loop
variable. The update expression is given as statement
inside the body of loop.
Types of loops:
1. for loop
2. While loop
Counting loops: the loops that repeat a certain
number of times, python for loop is a counting
loop.
Conditional loops: the loops that repeat until a
condition is true, python while loop is a condition
loop.
Rohit Pandita Sr. Lecturer Computer Science HSS Purkhoo
1. While Loop
While Loop in Python is used to execute a
block of statement till the given condition is
true. And when the condition is false, the
control will come out of the loop. The
condition is checked every time at the
beginning of the loop.
Syntax
Initialization
while <test expression>:
Statements
//program to understand the working of
//while loop
i= 1 //initialization
while i< 6: //test expression
print (i) // iteration stmt
i=i+1 // update condition
Output
1 2 3 4 5
Rohit Pandita Sr. Lecturer Computer Science HSS Purkhoo
// program for sum of 1st N natural nos
n= int (input (“enter the limit of natural
Nos=”))
i=1
while i< n:
print(i)
Sum=sum+i
i=i+1
print (“sum of 1st”,n, “natural Nos =”’ sum)
Output
enter the limit of natural Nos
= 5
1 2 3 4 5
sum of 1st 5 natural Nos = 15
[Link] loop
A for loop is used for iterating over a
sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
Syntax:
for < variable > in <sequence>:
Statements to repeat:
Rohit Pandita Sr. Lecturer Computer Science HSS Purkhoo
// program
for a in [1,4,7] :
Print(a)
Output :
1
4
7
// program
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
output:
apple
banana
cherry
range() function in a for loop
for a in range (1,5):
print(a)
In the above loop, range (1, 5) will first
generate a list [1, 2, 3, 4] with which for
loop will work.
Output will be 1 2 3 4
Rohit Pandita Sr. Lecturer Computer Science HSS Purkhoo
// program to print table of any No.
a = int(input (“enter any No. =”))
for i in range(1, 10):
c=a*i
print(a, “*”,i,”=”,c)
Output:
enter any No.= 5
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
// program to print sum of natural numbers
//from 1 to 10.
Sum=0
for n in range(1,11):
print(n)
sum =sum +n
print(“sum of natural Nos from 1 to 10 =”’sum)
Rohit Pandita Sr. Lecturer Computer Science HSS Purkhoo
Output:
1
2
3
4
5
6
7
8
9
10
Sum of natural Nos from 1 to 10 = 55
// program to print sum of n natural numbers.
sum = 0
a=int(input("enter limit of series ="))
n=1
while n<=a:
sum=sum+n
print(n)
n=n+1
print("sum of natural Nos=",sum)
Rohit Pandita Sr. Lecturer Computer Science HSS Purkhoo
output
enter limitof series =10
1
2
3
4
5
6
7
8
9
10
sum of natural Nos= 55
Rohit Pandita Sr. Lecturer Computer Science HSS Purkhoo