Loop - Repetition of Task: Vinod Kumar Verma, PGT (CS), KV Oef Kanpur & Sachin Bhardwaj, PGT (CS), KV No.1 Tezpur
Loop - Repetition of Task: Vinod Kumar Verma, PGT (CS), KV Oef Kanpur & Sachin Bhardwaj, PGT (CS), KV No.1 Tezpur
com
What is Loop?
As we have already studied there are many
situation where we need to repeat same set
of tasks again and again. Like while writing
table of any number we multiply same
number from 1 to 10, multiplying number
from n to 1 to find the factorial, In real life,
to prepare parathas, suppose to prepare
your bag with all items, daily coming to
school again and again, etc. Please explore
more real life conditions where repetition of
task was done by you.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Pseudocode Reference
A. start
B. Input number(n) to print table
C. Let count=1
D. Display n * I
E. Add 1 to count
F. if count==10:
A. Stop
G. else:
A. Repeat from Step C
H. Stop
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
range() function
Before we proceed to for loop let us
understand range() function which we
will use in for loop to repeat the
statement to n number of times.
Syntax:
◦ range(lower_limit, upper_limit)
The range function generate set of values
from lower_limit to upper_limit-1
range() function
For e.g.
range(1,10) will generate set of values
from 1-9
range(0,7) will generate [0-6]
Default step value will be +1 i.e.
range(1,10) means (1,2,3,4,5,6,7,8,9)
range() function
To change the step value we can use third
parameter in range() which is step value
For e.g.
range(1,10,2) now this will generate
value [1,3,5,7,9]
Step value can be in –ve also to generate
set of numbers in reverse order.
range(10,0) will generate number as
[10,9,8,7,6,5,4,3,2,1]
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
range() function
To create list from 0 we can use
range(10) it will generate
[0,1,2,3,4,5,6,7,8,9]
>>>‟a‟ in „apple‟
True
>>>‟national‟ in „international‟
True
for loop
for loop in python is used to create a
loop to process items of any sequence
like List, Tuple, Dictionary, String
It can also be used to create loop of fixed
number of steps like 5 times, 10 times, n
times etc using range() function.
OUTPUT
10
OUTPUT
10
OUTPUT
for i in range(1,101):
print(i,end='\t')
Lab work
1. WAP to enter any number and find its
factorial
2. WAP to print the following fibonacci series
0, 1, 1, 2, 3, 5, 8,……..n terms
3. WAP to enter 10 number and find the sum
and average.
4. WAP to enter Lower_Limit, Upper_Limit
and find the sum of all odds and evens
number between the range separately
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
while loop
While loop in python is conditional loop
which repeat the instruction as long as
condition remains true.
It is entry-controlled loop i.e. it first
check the condition and if it is true then
allows to enter in loop.
while loop contains various loop
elements: initialization, test condition,
body of loop and update statement
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Example – break
for i in range(1,20):
if i % 6 == 0:
break
print(i)
print(“Loop Over”)
Example – continue
for i in range(1,20):
if i % 6 == 0:
continue
print(i, end = „ „)
print(“Loop Over”)
else: 7
8
9
print("Loop Over") 10
Loop Over
4
to
8