Iteration Statements in Python
Iteration Statements in Python
● while Statements
● pass Statement
● Nested Loop
QUESTIONS
2. Describe the syntax of while loop with simple example codes in Python.
3. Describe the syntax of for loop with simple example codes in Python.
5. Draw the flowchart of while and for loop explaining their control flow.
7. Explain range() function. How do you use a for loop with range () function in Python?
Iteration or loop comes in situations where the same task has to be repeated or some set of instructions have to
be executed repeatedly, either a specific number of times or until a particular condition is satisfied or not
satisfied.
Python programming language provides following types of loops to handle looping requirements:
1. while
2. for
3. Nested
While Statements
● A while loop is used to repeatedly execute a block of code as long as a certain condition is true.
● Use while loop when you don't know how many times you need to execute the loop beforehand.
n=int(input("Enter n : "))
i=1
print("Printing Numbers upto ",n)
while i<=n:
print(i)
i=i+1
Output:
Enter n : 11
Printing Numbers upto 11
1
2
3
4
5
6
7
8
9
10
11
Code : Printing the even numbers till n.
n=int(input("Enter n : "))
i=1
print("Printing Even Numbers upto",n)
while i<=n:
if i%2==0:
print(i)
i=i+1
Output:
Enter n : 10
Printing Even Numbers upto 10
2
4
6
8
10
Code : To find the sum of first n natural numbers using a while loop.
n=int(input("Enter n : "))
sum=0
i=1
while i<=n:
sum=sum+i
i=i+1
print("The sum is", sum)
Output:
Enter n : 10
The sum is 55
mylist=[2,4,6,8,10,12,14,16]
print(len(mylist))
print(mylist)
index=0
while index<len(mylist):
print("Index is : " , index , "Elements are: " ,mylist[index])
index=index+1
Output:
8
[2, 4, 6, 8, 10, 12, 14, 16]
Index is : 0 Elements are: 2
Index is : 1 Elements are: 4
Index is : 2 Elements are: 6
Index is : 3 Elements are: 8
Index is : 4 Elements are: 10
Index is : 5 Elements are: 12
Index is : 6 Elements are: 14
Index is : 7 Elements are: 16
mylist=[2,2,2,5,2,3,6,8,9,10]
print(len(mylist))
print("List : ", mylist)
a=int(input("enter a number :"))
i=0
m=0
while i<len(mylist):
if a==mylist[i]:
print("number at" ,i+1,"th position")
print("number at" ,i,"th Index")
m=m+1
i+=1
if m==0:
print("number is not found")
Output :
10
List : [2, 2, 2, 5, 2, 3, 6, 8, 9, 10]
enter a number :5
number at 4 th position
number at 3 th Index
BREAK AND CONTINUE STATEMENTS
The break statement is used to exit from a loop immediately without completing the rest of the iterations.
SYNTAX:
In while loop,
In for loop,
n=int(input("Enter n : "))
i=1
print("Printing Numbers upto",n)
while i<=n:
print(i)
if(i==5):
break
i=i+1
Output:
Enter n : 10
Printing Numbers upto 10
1
2
3
4
5
In while loop,
In for loop,
n=int(input("Enter n : "))
i=1
print("Printing Numbers upto",n)
while i<=n:
if(i==5):
i=i+1
continue
print(i)
i=i+1
Output:
Enter n : 10
Printing Numbers upto 10
1
2
3
4
6
7
8
9
10
Code: Example of break statements (Multiplication table of 5 )
i=0
while True:
i=i+1
print("5*",i,"=",(5*i))
if i>=10:
break
Output:
5* 1 = 5
5* 2 = 10
5* 3 = 15
5* 4 = 20
5* 5 = 25
5* 6 = 30
5* 7 = 35
5* 8 = 40
5* 9 = 45
5* 10 = 50
Code : To print the even numbers upto n, which is highest number entered by the user
Output:
Enter value of n : 10
Printing even numbers upto 10
2
4
6
8
10
NOTE:Both break and continue are used with while loop statements used to stop the normal execution in
loops or iterations. The break statement exits from the complete loop if a condition is met. Whereas, the
continue statement causes execution to terminate the current iteration and immediately continue at the start of
the loop and start the next iteration. Thus, continue only skips the current iteration while break skips the
complete loop.
NOTE:The break statement terminates the execution of the loop whereas the continue statement skips
the particular iteration and proceeds to the next iteration and does not terminate the loop completely.
Another option in Python for implementing iteration or loop is the for statement. The syntax is shown below:
Code: 1
mylist=[2,4,6,8,10,12,14,16]
print(len(mylist))
print(mylist)
for elements in mylist:
print(elements)
Output:
8
[2, 4, 6, 8, 10, 12, 14, 16]
2
4
6
8
10
12
14
16
Code:2
str="WELCOME"
i=1
for letter in str:
print(i,"Letter : ",letter)
i=i+1
Output:
1 Letter : W
2 Letter : E
3 Letter : L
4 Letter : C
5 Letter : O
6 Letter : M
7 Letter : E
The range() function is a built-in function in Python that is used in for loop function for generating a sequence
of numbers which can be assigned to the loop variable.
In other words, Range functions return a sequence of numbers, starting from 0 by default, and increments by 1
(by default), and stops before a specified number.
range(end)
for i in range(5):
print(i)
Output:
0
1
2
3
4
The function range (a, b) will print from a to b-1 (excluding b) in order with default step size of 1.
range(start,end)
Code:
for i in range(5,10):
print(i)
Output:
5
6
7
8
9
To specify step size, we can add step size or incrementing value as the third argument to range(). Check
Code where it prints 1 to 9 with a gap of 3. Hence, the values are 1, 4, 7.
Code:
for i in range(1,10,3):
print(i)
Output:
1
4
7
NOTE: range() is a built-in function in Python to generate a range of numbers. It takes a set of values
consecutively that successively increase by a specified step size, or 1 by default, if not specified in the
function.