# Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

## "Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Loop Concept in Python:-

It is used to print range based data using a common repeatable block. It is also called iterative statement because it is used to enumerate data of LIST, Tuple, Dictionary, Set, and Database objects.

for example, if we want to print data from 1 to 1000 then we will prefer a loop statement.

1) While Loop Example in Python:-

2) For Loop in Python:-

3) Nested For Loop in Python with Example:-

Type of Loop:-

1)  while loop and while--else loop:-

it will provide all statements of loop separately and it is mainly used to create an infinite condition-based program.

init;
while(condition):
Statement
Increment

Forward                        Backward

i = min                          i=max

i<= max                        i>=min

i = i+1                          i=i-1

WAP to print 1 to 10?

i=1
while(i<=10):
print(i)
i=i+1

WAP to print 10 to 1?

i=10
while i>=1:
print(i)
i=i-1

Flow of Loop

1    Forward     i=min      i<=max      i=i+1

2    Backward   i=max     i>=min      i=i-1

3     Infinite       i=min,max  ,no condition, i=i+1,i=i-1

WAP to print a table of any entered number?

num=int(input("enter number to caculate table"))
i=1
while(i<=10):
print(str(num) + "*"+ str(i) + "=" + str(num*i))
i=i+1

WAP to print 1 to 5 and  5 to 1 using a single while loop?

1
2
3
4
5
5
4
3
2
1
Solution:-

i=1
while(i<=10):
if i<=5:
print(i)
else:
print(11-i)
i=i+1

WAP to calculate square and cube of one digit positive number?

1      1       1
2      4       8
3      9      27
...

Solution:-

start = int(input("enter starting point"))
end = int(input("enter ending point"))

i=start
while(i<end):
s=i*i
c=i*i*i
print(i,s,c)
i=i+1

Assignment of While Loop

## WAP to check prime number?

The solution to this program:-

num = int(input("enter number to check prime"))
i=2
count=0
while i<num:
if num%i==0:
count=1
break
i=i+1

if count==0:
print("prime")
else:
print("not prime")

## WAP to find max digit, second max digit in mobile number?

a= 681236783
8
7
The solution to this program:-

num = int(input("enter mobile number"))
m=0
sm=0

while(num!=0):
rev = num%10
print(rev)
num = num//10
if m<rev:
sm=m
m=rev
elif sm<rev and sm!=m:
sm=rev

print(m,"",sm)

Example of While else:-

else statement always used with if statement but in a python script, we can use else statement with a while and for loop both. It will be executed when the loop condition will be false.

#while else example

num = int(input("enter number"))
s=''
while num!=0:
s=s+str(num%10)
num=num//10
else:
print(s)

WAP to calculate factorial of any number?

num = int(input("enter number to calculate factorial"))
f=1
i=num
s=''
while i>1:  # i=num;i>1;i--
f=f*i  #f=5 20  60 120
s=s+str(i)+"*"
i=i-1

print("factorial is ",s+"1=",f)

2)   for loop and for..else:-

using this we will print all sub statements using a single statement with range().
range() is used to provide starting and ending point to process loop.

for i in range(min,max):     #forward  increment by default 1
statement

for i in range(max,min,-1):  #backward direction
statement
...........................................................................................................

WAP to print 1 to 10?

for i in range(1,11):
print(i)

WAP to print 10 to  1?

for i in range(10,0,-1):
print(i)

WAP to print a table of odd numbers?

num=int(input("enter number"))
if num%2!=0:
for i in range(1,11):
k=num*i
print(k)
else:
print("enter only odd number")

WAP to display fibonacci series?

#WAP to print fibonacci series 0 1 1 2 3 5 8 13 21
a=-1
b=1
for i in range(1,11):
c=a+b           #0 1 1 2 3
print(c)
a=b              #1
b=c              #1

WAP to calculate factorial of any number with complete expression?

#WAP to print factorial of any entered number?

num = int(input("enter number to calculate factorial"))
f=1
s=''
for i in range(num,1,-1):  # i=num;i>1;i--
f=f*i  #f=5 20  60 120
s=s+str(i)+"*"

print("factorial is ",s+"1=",f)

For..else:-

It will be executed when for loop condition will false.

for i in range(min,max):
Statements
else:
Statements

Example of for--else

WAP to calculate the sum of one digit positive number?

sum=0
for i in range(1,10):
sum=sum+i
else:
print("Total is ",sum)

Nested For Loop:-

Using this we will write more than one for loop using nested sequence, It is a collection of Outer for loop and inner for loop.

Outer for loop will execute once and inner for loop will be executed until the condition is false. when the outer loop condition will false then the loop program will be terminated.

for i in range(start,end):  row
for j in range(start,end):  column
Statement

NewLineStatement

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
...............................................................................................

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Solution:-

for i in range(5,0,-1):
for j in range(1,i+1):
print(j,end=' ')
print()

........................................
I  j
1   5
2   4
3   3
4   2
5   1

for i in range(1,6):
for j in range(1,7-i):

A B C D E
A B C D
A B C
A B
A
.............................................................

A
A B
A B C
A B C D
A B C D E

.....................................
I j
1 1
2 2
3 3
4 4
5 5

for i in range(1,6):
for j in range(1,7-i):

Assignment of Nested For Loop In Python:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
............................................

A B C D E
a  b  c  d
A B C
a b
A

The solution to this pattern:-

for i in range(70,65,-1):
for j in range(65,i):
if i%2==0:
print(chr(j),end=' ')
else:
print(chr(j+32),end=' ')
print()

...........................................................

A  a B b C
A a B b
A a B
A a
A

The solution to this program:-
for i in range(70,65,-1):
k=65
for j in range(65,i):
if j%2==0:
print(chr(k+32),end=' ')
k=k+1
else:
print(chr(k),end=' ')

print()

.........................................................

1 2 3 4 5
5 4 3 2
1 2 3
5 4
1

The solution to this pattern:-

for i in range(1,6):
for j in range(1,7-i):
if i%2==0:
print(6-j,end=' ')
else:
print(j,end=' ')
print()

......................................................................
1 0 0 1 0
1 0 0 1
1 0 0
1 0
1
...............................................................................

The solution of This program:-

for i in range(1,6):
a=1
b=0
for j in range(1,7-i):
if j%2==0:
print(b,end=' ')
b=1
else:
print(a,end=' ')
a=0
print()

Create a Pattern to print this pattern:-

A B C D E
B C D E
C D E
D E
E

The solution to this program:-

for i in range(0,5):
ch=65
for k in range(0,i):
print(' ',end=' ')
ch=ch+1
for j in range(i,5):
print(chr(ch),end=' ')
ch=ch+1

print()

...........................................

*
*      *       *
*     *       *       *     *
*       *      *
*

..............................

Infinite Loop in Python:-

This loop will continuously be executed until the condition false.we always prefer while loop for infinite condition and for loop for the finite condition.

Syntax of While Loop:-

while(true):
Statement

Example of Simple While Loop for the infinite condition?

while True:
print("*")

This program print infinite * symbols on the output screen

How we can convert the Infinite loop to stop mode?

We can ask user's input or last limit and convert true condition to false condition.

Example of this

flag=True
i=1
while flag:
print("*")
i=i+1
if i>10:
flag=False

in this program we convert infinite loop to finite when I value will be 10 then the loop will be automatically terminated.

Minor project work on Loop Statements?

ATM:-

WAP to manage ATM operation with the following instruction

1)  When an application will be loaded then ask for pin code, if the pin code is correct then the process to the next phase otherwise asks pin code three times if it will be wrong then the application will be closed automatically.

2)   If pin code is correct then ask for operation using menu  Press C for Credit, D for Debit, B for check balance, E for the exit, R for a Repeat operation.

3)   If the user performs three operations then automatically exit from the application.

4)  Display all log means the internal process

If you have any doubt in programming or join online classes then you can contact us by comment .

1. #print 10 to 1
print(" Decrease value 10 -1 ")
i=10
while (i>=1):
print(i)
i=i-1
print("\n*************")
print ( "Increase value 1-10 ")
#print 1 to 10
i=1
while (i<=10):
print(i)
i=i+1

2. #print 5 to 1 and 1 to 5 programm
print(" Decrease value 10 -1 ")
i=5
while (i>=1):
print(i)
i=i-1
print("\n*************")
print ( "Increase value 1-10 ")
#print 1 to 5
i=1
while (i<=5):
print(i)
i=i+1

3. #perform the addition of a digit positive number? 1 to 10 = 55
i=1
sum=0
while (i<=10):
sum = sum + i
print(i)
i=i+1
print("------------------------")
print ("The Sum is " ,sum)

4. for i in range(5,0,-1):
for j in range(1,i+1):
print(i,end=' ')
print()

######
Output :
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1

5. Write a program to calculate factorial number

n = int(input("Enter the number:"))
result = 1

for i in range(n,0,-1):
result = result*i
print("factorial of",n,"is",result)