0% found this document useful (0 votes)
13 views56 pages

LOOP Revised

The document explains the concept of loops in programming, specifically in Python, detailing how to repeat tasks using 'for' and 'while' loops. It covers the use of the 'range()' function for generating sequences of numbers and includes examples of loop structures and their applications. Additionally, it discusses the 'else' clause in loops, which executes when a loop terminates normally.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views56 pages

LOOP Revised

The document explains the concept of loops in programming, specifically in Python, detailing how to repeat tasks using 'for' and 'while' loops. It covers the use of the 'range()' function for generating sequences of numbers and includes examples of loop structures and their applications. Additionally, it discusses the 'else' clause in loops, which executes when a loop terminates normally.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Loop – repetition of

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)

Example – for loop


with Tuple
Code=(10,20,30,40,50,60)
for cd in Code:
print(cd)
Let us understand how for loop
works
Code=(10,20,30,40,
50,60)
for cd in Code:
print(cd)
First it will create Tuple
„Code‟
Code=(10,20,30,40,
50,60)
for cd in Code:
print(cd)
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)
for cd in Code:
print(cd)
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: 10
print(cd)
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: 10
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

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 FOR LOOP WILL


20 AUTOMATICALLY
ENDS WHEN LAST
AND SO ON… ITEM OF LIST IS
PROCESSED
for loop with
string
for ch in "Plan":
print(ch)

The above loop product


output P
l
a

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')

** here end=„\t‟ will cause output to


appear without changing line and give
one tab space between next output.
Program to print table of any
number
num = int(input("Enter any
number ")) for i in
range(1,11):
print(num,'x',i,'=',num*i)

Program to find the sum of all


number divisible by 7 between 1
to 100
sum=0
for i in
range(1,10
1): if i % 7
== 0:
Lab
1. work
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
5. WAP to find the square of odd
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
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
Example of simple
while
i=1 loop
while i<=10:
print(i)
i+=1
Example of simple
while
i=1 loop
initializati
on
while i<=10: Test
condition
print(i) Body of
i+=1 loop
U
p

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

In this way loop will


execute for the values 4 to
10, let us see from the
value 9
Let us see the flow of
loop
i=1
while i<=10:
print(i)
i=9
OUTPUT
---------- i+=1
1
2
3
4
5
6 In this way loop will
7
8
execute for the values 4 to
10, let us see from the
value 9
Let us see the flow of
loop
i=1
while i<=10: i=9
9<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
Let us see the flow of
loop
i=1
while i<=10: i=9
9<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
9
Let us see the flow of
loop
i=1
while i<=10:
print(i)
i=10
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
9
Let us see the flow of
loop
i=1
while i<=10: i= 10
10<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
9
Let us see the flow of
loop
i=1
while i<=10: i = 10
10<=10?
print(i) True
OUTPUT
---------- i+=1
1
2
3
4
5
6
7
8
9
10
Let us see the flow of
loop
i=1
while i<=10:
print(i)
i=1
OUTPUT
---------- i+=1 1
1
2
3
4
5
6
7
8
9
10
Let us see the flow of
loop
i=1
while i<=10: i= 11
11<=10?
print(i) False
OUTPUT
---------- i+=1
1
2
3
4 Now the value of i
5 is 11 and condition
6
7
will return False
8 so loop will
9 terminate
10
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 sum
WAP to enter any number and find its
reverse

num = int(input("Enter any


number ")) rev = 0
num2=nu
m while
num>0:
rem =
num
% 10
rev = rev * 10 +
WAP to enter numbers as long as user wishes to
enter and find the sum highest and lowest
number
choice= entered by user
"y" i=1
while
choice=
="y":
n
u
m
=
in
t(i
n
p
ut
("
E
nt
er
a
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
Syntax of Python loop
along 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)
Example (“else” with
while)
i=1
Output
while i<=10: 1
2
print(i) 3
4
i+=1 5
6

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

Enter any number :23


23 is a prime
number

Enter any number :36


23 is not a prime
number

You might also like