Loop
Loop
task
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
VINOD KUMAR VERMA, PGT(CS), KV OEF
task was done by you.
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
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 * count
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
for more updates visit:
www.python4csip.com
Python Loop
Statements
⚫ To carry out repetition of
statements Python provide 2
loop statements
◦ Counting loop (for)
◦ Conditional loop (while)
range()
function
⚫ Before weproceed 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 VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
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. VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
range()
function
⚫ To create list from 0 we
can use generat
⚫ range(10) it will e
[0,1,2,3,4,5,6,7,8,9]
consider the following statement and
predict the values generate:
(i)range(10)
(ii)range(5,10,2)
(iii) range(10,1,-3)
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Operators in and
not
⚫ The in
operator in and not in is
used in for loop to check whether
the value is in the range / list or
not
⚫ For e.g.
>>> 5 in [1,2,3,4,5]
True
>>> 5 in [1,2,3,4]
False
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
>>>‟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.
10
10
for with
range()
Let us create a loop to print all the natural
number from 1 to 100
for i in
range(1,10
1):
print(i,end=
'\t')
for i in range(1,21,2):
print(i,end=’ ‘)
Consider the loop given below and predict the final value of i after
this loop:
for i in range(7, 4, -2): OUTPUT:
break 7
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
VINOD KUMAR VERMA, PGT(CS), KV OEF
between the range separately
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
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 statementVINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
while loop
elements
1. Initialization : it is used to give
starting value in a loop variable
from where to start the loop
2. Test condition : it is the
condition or last value up to which
loop will be executed.
3. Body of loop : it specifies the
action/statement to repeat in the
loop
4. Update statement : it is the
increase or decrease in loop variable
to reach the test condition.
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
t
VINOD KUMAR VERMA, PGT(CS), KV OEF
e KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Programs of while
loop
⚫ Convert all "for‟ loop program with while
loop
⚫ WAP to enter any number and find its
reverse
⚫ WAP to enter any number and a digit
and count how many times digit is in
the number
⚫ WAP to enter any number and
check it is armstrong or not
⚫ WAP to enter any number and
check it is palindrome or not
⚫ WAP to enter numbers as long as user
wishes to enter and find the sumPGT(CS),highest
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, KV
for more updates visit:
www.python4csip.com
i=10
while i>0:
print(i,end=’$’)
i-=3
OUTPUT:
10$7$4$1$
x=10 OUTPUT:
y=0 10 0
while x>y: 91
print(x,y) 82
x-=1 73
y+=1 64
for more updates visit:
www.python4csip.com
Example –
forbreak
i in range(1,20):
if i % 6 == 0:
break
print(i)
print(“Loop
Over”)
The
above code
produces
output
1
2
3
4
SACHIN BHARDWAJ, PGT(CS), KV NO.1
5
for more updates visit:
www.python4csip.com
Example –
continue
for i in
range(1,20):
if i % 6 == 0:
contin
ue print(i,
end = „ „)
print(“Loop
Over”)
The above
code produces
output
123457
VINOD KUMAR VERMA, PGT(CS), KV OEF
8 9 10 11 13 14 KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Loop .. else ..
Statement
⚫ Loop in python provides else clause
with loop also which will execute
when the loop terminates
normally i.e. when the test
condition fails in while loop or when
last value is executed in for loop
but not when break
terminates the loop
with
„else‟ clause
for with “else” while with “else”
for <var> in <seq>: while <test
statement 1 condition>:
statement 2 statement 1
else: statement 2
statement(s) else:
statement(s)
("Loo
p
VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Generating Random
numbers
⚫ Python allows us to generate
random number between any
range
⚫ To generate random number we
need to import random library
⚫ Syntax: (to generate random
number)
◦ random.randint(x,y)
◦ it will generate random number
between x to y
◦ For e.g. : VINOD KUMAR VERMA, PGT(CS), KV OEF
KANPUR & SACHIN BHARDWAJ, PGT(CS), KV
for more updates visit:
www.python4csip.com
Syntax of Nested
LoopLoop – FOR
Nested Nested Loop – WHILE
4
to
8