You are on page 1of 10

राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी

कारैक्काल– 609 609


NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609

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.

 This allows you to automate repetitive tasks, process large amounts of


data efficiently, and perform operations on collections of items without
needing to write redundant code.

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 Loop Syntax:

while expression:

statement(s)
राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609

Figure 1a: Flow Chart for While Loop

Example flow chart:


राष्ट्रीय प्रौद्योगिकी संस्थान पुदुच्चेरी
कारैक्काल– 609 609
NATIONAL INSTITUTE OF TECHNOLOGY PUDUCHERRY
Karaikal – 609 609

Figure 1b: Example Flow Chart for While Loop

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.

for iterator_var in sequence:


statements(s)

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

for iterator_var in sequence:


statements(s)
statements(s)
The syntax for a nested while loop statement in the Python programming
language is as follows:
while expression:
while expression:
statement(s)
statement(s)

from __future__ import print_function


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

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

You might also like