LOOP Revised
LOOP Revised
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 task was done
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 D
H. Stop
Python Loop
Statements
To carry out repetition of
statements Python provide 2
loop statements
◦ Conditional loop (while)
◦ Counting loop (for)
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
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()
function
To create list from 0 we
can use genera
range(10) it will te
[0,1,2,3,4,5,6,7,8,9]
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
Program to check whether
any word is a part of
sentence or not any
line = input("Enter
statement") word =
input("Enter any word")
if word in line:
print("Yes ", word, "is a part
of ",line) else:
print("No", word ," is not
part of ",line)
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.
Example – for loop
with List
School=["Principal","PGT","T
GT","PRT"] for sm in School:
print(sm)
10
Then for loop starts, it will pick
values one by one from Code
and assign it to
Code=(10,20,30,40,cd
50,60) cd =
for cd in Code: 20
print(cd)
OUTPUT
10
Then for loop starts, it will pick
values one by one from Code
and assign it to
Code=(10,20,30,40,cd
50,60) cd =
for cd in Code: 20
print(cd)
OUTPUT
n
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')
e
Let us see the flow of
loop
i=1
while i<=10:
print(i)
i+=1
Let us see the flow of
loop
i=1
while i<=10: i=1
1<=10?
print(i) True
i+=1
Let us see the flow of
loop
i=1
while i<=10: i=1
1<=10?
print(i) True
OUTPUT
---------- i+=1
1
Let us see the flow of
loop
i=1
while i<=10:
print(i)
i=2
OUTPUT
---------- i+=1
1
Let us see the flow of
loop
i=1
while i<=10: i=2
2<=10?
print(i) True
OUTPUT
---------- i+=1
1
Let us see the flow of
loop
i=1
while i<=10: i=2
2<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
Let us see the flow of
loop
i=1
while i<=10:
print(i)
i=3
OUTPUT
---------- i+=1
1
2
Let us see the flow of
loop
i=1
while i<=10: i=3
3<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
Let us see the flow of
loop
i=1
while i<=10: i=3
3<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
Let us see the flow of
loop
i=1
while i<=10:
print(i)
i=4
OUTPUT
---------- i+=1
1
2
3
else: 7
8
9
print(" 10
Loop
Loop Over
Over")
Example (“else”
with for)
names=["allahabad","lucknow","varanasi","kanpur","agra","ghaziabad"
,"mathura","meerut"]
city = input("Enter city to search: ")
for c in names:
if c == city:
print(“City Found")
break Output
else: Enter city to search :
varanasi
print("Not found") City Found
Enter city to search :
unnao
Not found
Example- program to enter numbers
repeatedly and print
their sum. The program ends when the user
says no more to enter(normal termination) or
program aborts when the number entered is
less than zero
count = sum = 0
ans = 'y'
while ans=='y':
num = int(input("Enter
number :")) if num < 0:
print("You entered
number below zero,
Aborting...")
brea
k sum +=
num
count+=1
ans
Example- program to enter numbers repeatedly
and print
their sum. The program ends when the user
says no more to enter(normal termination) or
program aborts when the number entered is
less than zero
Enter Enter number
number :3 :10
Continue(y/n)y Continue(y/n)
y Enter
Enter number
number :20
:6 Continue(y/n)
Continue(y/n)y y Enter
Enter number number :50
:8 Continue(y/n)
Continue(y/n)y n
Enter number You entered 3 numbers
:-10 so far Sum of entered
You entered number number is : 80
below zero,
Aborting...
Sum of entered
Example- program to input
number and tests if it is a
prime number or not
num = int(input("Enter any
number :")) lim = num//2 + 1
for i in
range(2,lim
): rem =
num % i if
rem == 0:
print(num,"
is not a
prime
number ")
Example- program to input
number and tests if it is a
prime number or not - OUTPUT