Loops in Python
Lecture #10
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
Airplane Airplane Airplane Airplane Airplane Airplane
2
What a R e Loops ?
Consider I want to print my name 10 times.
print("Fahad")
print("Fahad")
print("Fahad")
print("Fahad")
print("Fahad")
print("Fahad")
OR print("Fahad")
print("Fahad")
print("Fahad")
print("Fahad")
print("Fahad")
3
“
A loop is used to repeat an
instruction multiple times
until a condition goes wrong.
4
Types of Loops in Python
There are two primitive loop types in Python –
For in While
loop loop
5
FoR in Loops
A for in loop is used for iterating over a sequence (that is either a
list, a tuple, a dictionary, a set, or a string)
Syntax –
for number in sequence :
statement(s)
6
While Loops
A while loop is used to execute a set of statements as long as a
condition is true.
Syntax –
while condition :
statement(s)
7
# for in loop
iterable_objs = range(6)
for iterator in iterable_objs:
print(iterator)
# while loop
i = 42
while not(i<=10):
print(i)
i -= 7
8
The else Statement
With the else statement, we can run a block of code once a
condition is no longer true.
Syntax –
while condition :
statement(s)
else :
statement(s)
9
Loop Cont R ol S tatements
Loop control statements change execution from its normal
sequence. There are 3 types –
break continue pass
statement statement statement
10
# Break
for x in range(6):
if x==4:
break
print(x)
# Continue
for y in "Abhay":
if y=='h':
continue
print(y)
# Pass
mylist = [1,2,5,8,6,3]
for c in mylist:
pass
11
Points to be noted
For in loop default increments by 1.
Else statement in loop won’t work if the loop is terminated
by break statement.
Nested loop is a type of non primitive loops in python.
In this, we can use multiple for or while loops inside a for or
while loop.
12
Let’s R eview Today’s Lea R ning
What is Loops in Python? Benefits of using Loop Types of Loop in Python
In this, we have understood the Here we have gone through some We have learned the two primitive
basic meaning of loop and have examples which cleared why loops types of loops in Python and their
gone through definition of loops. are efficient and less time implementation.
consuming.
Else statement in Loop Loop Control Statements Points to be noted
Here we have learned about the else Here, we have gone through the Here we have covered some extra
statement. It’s implementation and three different loop statements in points related to our topics.
where it works. Python. How and where they are
used.
13
thanks! thanks! thanks! thanks! thanks! thanks! thanks!
thanks! thanks! thanks! thanks! thanks! thanks! thanks!
thanks! thanks! thanks! thanks! thanks! thanks! thanks!
thanks! thanks! thanks! thanks! thanks! thanks! thanks!
thanks! thanks! thanks! thanks! thanks! thanks! thanks!
thanks! thanks! thanks! thanks! thanks! thanks! thanks!
thanks! thanks! thanks! thanks! thanks! thanks! thanks!
thanks! thanks! thanks! thanks! thanks! thanks! thanks!
thanks! thanks! thanks! thanks! thanks! thanks! thanks!
14