Loops
Loops
v1,v2=5,10
for x in range(1,4):
v1=v1+1
v2=v2-1
print(v1,v2)
print(v2-1,v1+1)
Output:
69
87
78
78
87
69
CONCEPT OF
BREAK
AND
CONTINUE STATEMENT
IN PYTHON
Break statement is used to exit
immediately from a loop.
This work for both for and while
loops.
If you are using nested loops then
break statement will stop the
execution of
innermost loop And start executing
the next line of code after the block.
Continue : this statement is used to
skip the block of the code in a for
And while loops.
A continue is shortcut which take the
Control to the loop without executing
the rest statement inside the loop
Please watch this video for the practical
implementation of
break and continue….
Find the output :
Testing
condition
Body of
The loop
Increment part
This print statement if not part of while loop Of the loop
Program.
WAP to obtain a number
and then generate
even number up to this
number using while loop .
n = int(input("enter a number "))
i=2
while i<=n:
print(i)
i+=2
Converting for to while loop and vice versa
b o a r d e x a m s
p p a r t fo r
v.Im
Video
FIND THE OUTPUT AND CONVERT THE CODE USING FOR LOOP
n1=0
n2=1
c=1
while c<=12:
print(n1,"@",n2)
n1+=n2
n2+=n1
c+=2
print("c = ",c)
n1=0
n2=1
for c in range(1,12,2):
print(n1,"@",n2)
n1+=n2
n2+=n1
print("c = ",c)
FIND THE OUTPUT
w=10
i=0
while w<20:
print(w*i)
i+=2
if i==8:
w=20
else:
w=w-3
FIND THE OUTPUT AND CONVERT THE CODE USING FOR LOOP
i=x=0
while i<20:
if i%5==0:
x=x+1
print(x)
i=i+1
x=x+1
print(x)
FIND THE OUTPUT if n=11 and n=14
n=int (input(“enter a num”))
i=2
while n%i!=0:
i=i+1
print(i)
FIND THE OUTPUT
n=7583241
f=s=0
while n>0:
r=n%10
if r %2!=0:
f+=r
else:
s+=r
n=n//10
print(f)
print(s)
print(f-s)
FIND THE OUTPUT
n=42
a=m=0
c=1
while n>0:
a=n%2
m=m+(a*c)
c=c*10
n=int(n/2)
print(m)
CONCEPT OF INFINITE LOOP :
Inner loop
for i in range(1,6): *
**
for j in range(i): ***
print("*",end=' ') ****
*****
print()
NESTED WHILE LOOP
i=6
while(i>0):
j=6 *
while(j>i): **
print("*",end=' ') ***
j-=1 ****
i-=1 *****
print()
WRITE PYTHON TO CODE PRINT THE FOLLOWING PYRAMID :
PYTHON CODE
for i in range(1,7,2):
for j in range(i):
print("*",end=' ')
print()
for i in range(1,6):
for j in range(i):
print(i,end=' ')
print()
for i in range(1,6):
for j in range(5,i-1,-1):
print(j,end=' ')
print()
for i in range(6,0,-1):
for j in range(1,i):
print(j,end=' ')
print()
Q-2
Q-1
Q-2
Q-3
Q-4
Q-3
Q-4
Q-5
Q-5
Q-6
PROGRAM FOR PRACTICE:
# 12345
1
### 2 4 6 8 10
13
##### 3 6 9 12 15
135
### 4 8 12 16 20
1357
# 5 10 15 20 25