Professional Documents
Culture Documents
Looping in Python
Looping in Python
1. LOOPING IN PYTHON
1.1 Purpose of Loop:
The purpose of iterating over a sequence of elements or executing a block
of code repeatedly until a certain condition is satisfied.
Types of Loop:
While
For
Nested Loop
1) While Loop
It is used to execute a block of statements repeatedly until a given
condition is satisfied.
When the condition becomes false, the line immediately after the loop in
the program is executed.
while expression:
statement(s)
राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609
a) Sample Program:
i=0
n=3
while (i < n):
i=i+1
print("Welcome Ist year ECE")
राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609
b) Sample Program:
i=0
n=3
while (i < n):
i=i+1
print("Welcome Ist year ECE")
else:
print("In else block")
c) Sample Program:
i=0
राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609
while (i==0):
print("Welcome Ist year ECE")
else:
print("In else block")
2) For Loop
For Loops are used for sequential traversal. For example: traversing
a list or string or array etc.
In Python, there is “for in” loop which is similar to foreach loop in
other languages.
a) Sample Program:
n=4
राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609
i=0
for i in range(i, n):
print(i)
b) Sample Program
print("Example with List,Tuple,String & Dict Iterate Using for Loops in
Python")
print("\n List Iteration")
l = ["Welcome", "to ", "ECE" ,"Ist", "Students"]
for i in l:
print(i)
print("\nTuple Iteration")
t = ("Hi", "Hello", "Welcome")
for i in t:
print(i)
print("\n String Iteration")
s = "students"
राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609
for i in s:
print(i)
print("\nDictionary Iteration")
d = dict()
d['name'] = 856
d['value'] = 1234
for i in d:
print("%s %d" % (i, d[i]))
print("\nSet Iteration")
set1 = {1, 2, 3, 4, 5, 6}
for i in set1:
print(i)
राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609
c) Sample Program:
list = ["a", "b", "c"]
print("length of list is",len(list))
for index in range(len(list)):
print(list[index])
else:
print("Inside Else Block")
3) Nested Loop
Python programming language allows to use one loop inside another loop
which is called nested loop. Following section shows few examples to
illustrate the concept.
Nested Loops Syntax:
for iterator_var in sequence:
राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609
Control statements
for letter in 'python':
if letter == 'p' or letter == 'x':
continue
print('Current Letter :', letter)
राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609