Loop in Python (While Loop)
Loop in Python (While Loop)
<initialisation>
while <condition expression>:
statement1
statement2
<increment>
Ex 1:
num =1 //Initialization
while num<= 5: //Condition
print("num =", num)
num = num + 1 // increment
Output:
num = 1
num = 2
num = 3
num = 4
num = 5
Output:
num = 1
num = 2
num = 3 …….will print till num=10
Same Ex 2 with a change :
WAP to print numbers from 1 to 10 with print
given before increment.
num =0 //Initialization
while num< 10: //Condition
print("num =", num)
num = num + 1 // increment
Output:
num = 0
num = 1
num = 2 …….will print till num=9
WAP to print even numbers from 1 to 10 .
n=0 //Initialization
while n< 10: //Condition
n=n+2 // increment
print("number =", n)
Output:
number = 2
number = 4
number = 6
number = 8
Number=10
Pr Prog. : WAP to print sum of even numbers till n.
WAP to print odd numbers from 1 to 10 .
num=1 //Initialization
while num< 10: //Condition
print("num =", num)
num = num + 2 // increment
Output:
num = 1
num = 3
num = 5
num = 7
num= 9
Pr Prog. : WAP to print sum of odd numbers till n.
Practice programs using while loop :
OUTPUT : 1 2 4 5
The else Statement :
With the else statement we can run a block of code
once when the condition no longer is true.
Ex 5 :
WAP to print a message once the condition is false.
i=1
while i<6:
print(i)
i=i+1
else:
print("i is no longer less than 6")
1
2
3
4
5
i is no longer less than 6
WAP to add two numbers and print sum, till the
user enters ‘n’ as response using break statement.
res="y"
while res=="y":
a=int(input("Enter first number"))
b=int(input("Enter second number"))
print (a+b)
res=input("Want to continue")
if res=="n":
break
else:
continue
Reverse while Loop with (–)ve step :
The general syntax is :
<initialization>
while <condition> :
<statements>
<negative step>
Ex: i=10
while i>=0 :
print (i)
i=i - 1
Practice programs :
-WAP to print numbers in reverse order from 20 to 1.
-WAP to print numbers in reverse order from (n) to 1.
Nested while Loop :
A nested loop is a loop that occurs within another loop,
structurally similar to nested for statements.
Syntax :
<initialization 1 >
while <condition 1> : <first loop>
<initialization 2>:
while <condition 2> : <second inner loop>
<statements>
<increment 2>
<increment 1>
Practice programs :
-WAP to print the Fibonacci series (0,1,1,2,3,5..) for n term
(number of elements to be generated not the end limit).
Using For Loop Using While Loop
1 to the power 1 is = 1
2 to the power 2 is = 4
3 to the power 3 is = 27
4 to the power 4 is = 256
5 to the power 5 is = 3125
Sum = 3413
WAP to print sum of the series : 11 + 22 + 33 + 44 …… nn
s=0
n=int(input("Enter the value of n"))
i=1
while i<=n :
p=1
j=1
while j<=i :
p=p*i
j=j+1
print (i,"to the power",i,"is =",p)
s=s+p
i=i+1
print(end=' ')
print ("sum =",s)
Output will be :
i=1
while i<=5 :
for i in range (1,6):
j=1
for j in range(1,i+1):
while j<=i:
print(j, end=" ")
print (j,end=' ')
print(" ")
j=j+1
print("\n")
i=i+1
WAP to print sum of the series :
s=0
x=int(input("Enter value of x"))
n=int(input("Enter the value of n"))
i=1
while i<=n :
p=1
j=1
while j<=i :
p=p*x
j=j+1
print (x,"to the power",i,"is =",p)
s=s+(1/p)
i=i+1
print(end=' ')
print ("sum =",s)
Output will be :
Enter value of x 2
Enter the value of n 4
2 to the power 1 is = 2
2 to the power 2 is = 4
2 to the power 3 is = 8
2 to the power 4 is = 16
sum = 0.9375