Python Loops
Loop is a structure used in programs to repeat the execution of a block for a
finite number of times. The execution may be stopped when a specific condition
becomes false or when it reaches maximum number of times.
Python has two types of loops
1. while loop
2. for loop
Python while loop
A while loop statement in Python repeatedly executes the block of statements
as long as the given condition is true.
Syntax
while (condition) :
statement 1
statement 2
.
.
statement n
The above syntax is similar to that of if statement. The block of statements in if
statement is executed only once when the condition is true, but the statements
in while loop is repeatedly executed until the condition becomes false.
Example 1
count=1
while (count < = 5):
print(count)
count+=1
print(“End of Loop)
Prepared by Dr. K.Akilandeswari
Output
1
2
3
4
5
End of Loop
Example 2
count=1
sum=0
while (count < = 5):
sum=sum+i
count+=1
print(“Sum of numbers = “,sum)
Output
Sum of numbers = 15
Example 3
flag =1
while (flag): print(“I Love Python)
print(“End of Loop”)
In the above example, there is only a single line in the body of the loop and
hence it can be placed in the same line. The value of the conditional variable
flag is initially 1 which is non-zero and considered as true. Its value is not at all
altered in the loop, so the loop will not terminate. Control key with C can be
used to break such an infinite loop.
Prepared by Dr. K.Akilandeswari
Points to remember
1. The condition may be any expression, and true is any non-zero value.
2. The while loop does not guarantee the execution of the loop block atleast
once. When the condition is tested and the result is false, the loop block
will be skipped and the next statement after the while loop will be
executed.
3. The value of the looping variable specified in the condition ( In the above
example, the variable count is the looping variable) should be altered so
that the loop will terminate when the condition evaluates to false.
Otherwise the loop will not terminate and would lead to infinite loop.
4. The statements indented by the same number of character spaces are
considered as a single block of code.
Python for loop
The for statement in Python is used to execute a block of statements repeatedly
for a fixed number of times. The execution of block of statements in a loop once
is called iteration. The variable which is used to control the loop execution is
called iteration variable.
The for statement in Python is used to iterate over the items of any sequence,
such as list, tuple, dictionary, set, or string. The block of statements will be
executed for each ieration.
Syntax
for iterator_var in sequence:
statements
Example using a list as sequence
languages = ['Python', 'C++', 'Java']
for lang in languages :
print(lang)
Prepared by Dr. K.Akilandeswari
Output
Python
C++
Java
The range ( ) function
The built-in function range() function is used to iterate over a sequence of
numbers. The range() function returns a number, starting from 0 and
increments by 1 and ends at a number.
For example,
range(5) returns 0,1,2,3,4 (5 is not included)
range(5,10) returns 5,6,7,8,9
range(1) returns 0
range(3,10,2) returns 3,5,7,9 (2 is the increment value)
range(3,20,3) returns 3,6,9,12,15,18
Example for loop with range()
for i in range(5):
print(i)
Output
0
1
2
3
4
Prepared by Dr. K.Akilandeswari
Else in For Loop
The keyword else can be used in a for loop to specify a block of code to be
executed when the loop is terminated.
Example
Print all numbers from even numbers from 2 to 20, and print a message when
the loop has ended.
for n in range(2,21,2):
print(n)
else:
print("end of loop")
Output
2
4
6
8
10
12
14
16
18
20
end of loop
Loop Control Statements
Looping Statements like while and for are used to execute a block of
statements iteratively until a condition is satisfied. There may be situation in
which we may like to exit from a loop or just skip an iteration in a loop. To
handle such situations, python uses loop control statements which can change
the normal flow of execution.
Prepared by Dr. K.Akilandeswari
The loop control statements in Python are
break
continue
pass
The break statement is used to terminate a loop when the given condition is met. It
is usually used with if statement inside the body of a loop. If the break statement
is inside a nested loop (loop inside another loop), the break statement will
terminate the loop where it is placed.
# Use of break statement inside the loop
for val in "string":
if val == "i":
break
print(val)
print("The end")
# Program to show the use of continue statement inside loops
for val in "string":
if val == "i":
continue
print(val)
print("The end")
Prepared by Dr. K.Akilandeswari