Sometimes we want to repeat a set of statements in our program, for instance : print 1 to 100
Loops make it easy for a programmer to tell the computer which set of instructions to be repeated and how !
Loops make it easy for a programmer to tell the computer which set of instructions to be repeated and how !
Types of Loops in Python :
1. While Loop
2. For Loop
2. For Loop
While Loop :
In while loop, the condition is checked first. If it evaluates to true then the body of the loop is executed else terminated.
In while loop, the condition is checked first. If it evaluates to true then the body of the loop is executed else terminated.
If the loop is entered, the process of is continues until the condition becomes false.
while condition :
#Body of the loop
i=0
while i<5
print("name")
i=i+1
prints name for 5 times
#Body of the loop
i=0
while i<5
print("name")
i=i+1
prints name for 5 times
**If the condition never becomes false, the loop keeps getting executed.
For Loop :
it is used to iterate through a sequence like list, tuple or string [iterables]
i = [1,7,8]
for item in l :
print (item)
prints 1, 7 and 8
Range function in Python :
it is used to iterate through a sequence like list, tuple or string [iterables]
i = [1,7,8]
for item in l :
print (item)
prints 1, 7 and 8
Range function in Python :
the range function in python is used to generate a sequence of numbers. we can also specify the start, stop and step-size :
range (start, stop, step-size)
range (start, stop, step-size)
**step-size usually not used with range ( )
for i in range (0, 7)
print (i)
** range(7) can also be used and pints 0 to 6
print (i)
** range(7) can also be used and pints 0 to 6
For Loop with else :
An optional else can be used with a for loop if th ecode is to be executed when the loop exhausts.
l = [1, 7, 8]
for item in l :
print (item)
else :
print("Done")
output :
1
7
8
done
for item in l :
print (item)
else :
print("Done")
output :
1
7
8
done
The Break statement :
break statement is used to come out of the loop when encountered. It instructs the program to Exit the loop now.
for i in range (0, 80) :
print (i)
if i==3:
break
for i in range (0, 80) :
print (i)
if i==3:
break
After printing 0, 1, 2, 3 the loop exit.
The Control Statement :
continue is used to stop the current iteration of the loop and continue with the next one. It instructs the program to skip this iteration.
for i in range (4) :
print ("printing")
of i==2 :
continue
print(i)
print ("printing")
of i==2 :
continue
print(i)
Pass Statement :
pass is a null statement in python. It instructs to do nothing.
l = [1, 7, 8]
for item in l:
pass
pass is a null statement in python. It instructs to do nothing.
l = [1, 7, 8]
for item in l:
pass
**without pass, the program will throw an error.
Programs :
Programs :
1. #While Loop
i = 0
while i<10:
print("Yes " + str(i))
i = i + 1
print("Done")
2. #Another While Loop
fruits = ['Banana', 'Watermelon', 'Grapes', 'Mangoes']
i = 0
while i<len(fruits):
print(fruits[i])
i = i+1
3. #Print List using for
fruits = ['Banana', 'Watermelon', 'Grapes', 'Mangoes']
for item in fruits:
print(item)
4. #range function
for i in range(1, 8, 1):
print(i)
5. #for else
for i in range(10):
print(i)
else:
print("This is inside else of for")
6. #break statement
for i in range(10):
print(i)
if i == 5:
break
else:
print("This is inside else of for")
7. #continue statement
for i in range(10):
if i == 5:
continue
print(i)
8. #Pass statement
i = 4
def run(player):
pass
def ouch(player):
pass
if i>0:
pass
while i>6:
pass
print("He is a good boy")