[go: up one dir, main page]

0% found this document useful (0 votes)
32 views28 pages

Looping Statements

The document discusses different types of looping statements in Python including for loops and while loops. It provides examples of using range(), break and continue statements, and how to create infinite loops. Common looping constructs like printing tables and calculating factorials are demonstrated.

Uploaded by

svajra2060
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)
32 views28 pages

Looping Statements

The document discusses different types of looping statements in Python including for loops and while loops. It provides examples of using range(), break and continue statements, and how to create infinite loops. Common looping constructs like printing tables and calculating factorials are demonstrated.

Uploaded by

svajra2060
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/ 28

LOOPING STATEMENTS

Looping statement are


the statements execute
one or more statement repeatedly several
number of times.
TYPES OF LOOPING STATEMENTS

 THERE ARE 2 LOOPING/ITERATIVE


STATEMENTS IN PYTHON
 WHILE LOOP
 FOR LOOP
range() function

The range() function generates a list which is a special sequence


type.
Syntax:
range(<lowerlimit>,<upperlimit>)
#both limits should be integers
this function in the form range(l,u)will produce a list having values
starting from l,l+1,l+2...u-1
but the lower limit is in list but upper limit is not included in the list
Example:range(0,5)
it will produce the list [0,1,2,3,4]
range (1,32,10)
it will produce the list[1,11,21,31]
STATEMENT VALUES GENERATED
range(10) (l=0,u=10) 0,1,2,3,4,5,6,7,89
range(5,10) 5,6,,7,8,9
range(3,7) 3,4,5,6
range(5,15,3) 5,8,11,14
range(9,3,-1) 9,8,7,6,5,4
range(10,1,-2) 10,8,6,4,2
in and not in operators-Membership
operators

 To check whether a value contained inside a list we can use in


operator
 example:
 3 in [1,2,3,4]
 will return True
 5 in [1,2,3,4]
 will return False
 5 not in [1,2,3,4]
 will return True
 “a” in “apple”
 will return True
 “ash”in “trash”
 will return True
ITERATION/LOOPING STATEMENTS

The iteration or repetition statements allows a


set of instructions to be performed
repeatedly until a certain condition is
satisfied
Python provides 2 kinds of loops :for loop and
while loop
for loop

The for loop is designed to process the items of


any sequence, such as list or a string one by
one.
for loop is a counting loops –the loop that
repeat a certain number of times
The general form of for loop is
for <variable> in <sequence>:
statements to repeat
Example:
for a in[10,40,20]:
print(a,a*a)
output:
10 100
40 1600
20 400
Working of for loop

 The loop variable is assigned the first value in


the sequence
 all the statements in the body of the for loop
are executed with assigned value of loop
variable
 the loop variable is assigned the next value in
the sequence an the loop body is executed(step
2 repeated)with the new value of loop-variable
 This continues until all values in the sequences
are processed.
Example

for c in “python”:
print(c)
output:
p
y
t
h
o
n
Example

for s in (1,2,3):
print(s+10)
output:
11
12
13
for loop with range function

for val in range(1,11,2):#[1,3,5,7,9]


print(val)
output:
1
3
5
7
9
output
x=20
y=5
for i in range(x-y *2): #(20-10)
#range(10) [0,1,2,3,4,5,6,7,8,9]
print(“%”,i)
output:
%0
%1
%2
%3
%4
%5
%6
%7
%8
%9
program to print table of a given number

n=int(input(“Enter any one number”))


for a in range(1,11):[1,2,3,4,5,6,7,8,9,10]
print(n,“X”,a,”=“,n*a)
output:
Enter any one number2
2 X 1 =2
2 X 2=4
......
2X10=20
program to find factorial of a given
number

num=int(input("enter a number")) #3
factorial=1
if num<0:
print("negative!Invalid")
elif num==0:
print(“factorial of “num
is ”, 1)
else:
for i in range(1,num+1):#[1,2,3]
factorial=factorial*i
print("factorial of ",num,"is",factorial)
while loop

The while loop is a conditional loop that will


repeat the instructions within itself as long as
condition remains True.
Syntax:
while<condition>:
loop body
 The loop-body may contain a single
statement or multiple statement or an empty
statement.
 The loop iterates while the logical expression
evaluates to True.When the expression
becomes false,the program control passes to
the line after the loop-body.
 Example:
a=5 #Initialization Expression
while a>0: #Test Expression
print(“welcome”,a) #Body of the loop
a=a-3 #UPDATE EXPRESSION
print(“Thank u!”)
output:
welcome 5
welcome 2
Thank u!
Elements of while loop

1. Initialization Expression
2. Test Expression
3. Body of the loop
4. Update Expression
EXAMPLE
nterms = int(input("How many terms? "))
n1, n2 = 0, 1
count = 0
while count < nterms:
print(n1)
nth = n1 + n2
n1 = n2
n2 = nth
count += 1
output:
How many terms? 6
0
1
1
2
3
5
Endless loop

The loop variable should be updated inside the body of the while in a
way that after some time the test condition becomes false otherwise
the loop will become an endless loop or infinite loop.
Example:
a=5
while a>0:
print(a)
print(“&&&&&”)
What is the output when the input is i)5
ii)0 ?

n=int(input("Enter N"))
i=1
sum=0
while i<n:
if i%2==0:
sum=sum+i
i=i+1
print(sum)
output

b) x=10
y=0
while x>y:
print(x,y)
x=x-1
y=y+1
c) keepgoing=True
x=100
while keepgoing:
print(x)
x=x-10
if x<50:
keepgoing=False
d) x=45
while x<50:
print(x)
e) for x in [1,2,3,4,5]:
print(x)
f)for x in range(5):
print(x)
g)for p in range(1,10):
print(p)
h)for q in range(100,50,-10):
print(q)
i)for z in range(-500,500,100):
print(z)
j)for y in range(500,100,100):
print("*",y)
k) x=10
y=5
for i in range(x-y*2):
print("%",i)

You might also like