Python Iterative Statements

Iteration statements or loop statements allow us to execute a block of statements as long as the condition is true.

Loops statements are used when we need to run same code again and again, each time with a different value.

Type of Iteration Statements In Python 3

In Python Iteration (Loops) statements are of three types :-

1. While Loop

2. For Loop

3. Nested For Loops


1. While Loop In Python


While Loop In Python is used to execute a block of statement as long as a given condition is true. And when the condition is false, the control will come out of the loop.

The condition is checked every time at the beginning of the loop.

While Loop Syntax

while (condition):

statements

Flowchart of While Loop

Python Flowchart of While Loop

Example Of While Loop

x = 0
while (x < 5):
   print(x)
   x = x + 1

Output :-

0
1
2
3
4

While Loop With Else In Python

The else part is executed if the condition in the while loop becomes False.

Syntax of While Loop With Else

while (condition):

loop statements 

else:

else statements

Example of While Loop With Else

x = 1
while (x < 5):
   print(‘inside while loop value of x is ‘,x)
   x = x + 1
else:
   print(‘inside else value of x is ‘, x)

Output :-

inside while loop value of x is 1
inside while loop value of x is 2
inside while loop value of x is 3
inside while loop value of x is 4
inside else value of x is 5

Infinite While Loop In Python

A Infinite loop is a loop in which condition always remain True.

Example of Infinite While Loop

x = 1
while (x == 1):
   print(‘hello’)

Output :-

hello
hello
hello
—–
—–


2. For Loop In Python


For loop in Python is used to iterate over items of any sequence, such as a list or a string.

For Loop Syntax

for val in sequence:

statements

Flowchart of For Loop

Python Flowchart of For Loop

Example of For Loop

for i in range(1,5):
   print(i)

Output :-

1
2
3
4

The range() Function In Python

The range() function is a built-in that is used to iterate over a sequence of numbers.

Syntax Of range() Function

range(start, stop[, step])

The range() Function Parameters

start: Starting number of the sequence. 
stop: Generate numbers up to, but not including this number. 
step(Optional): Determines the increment between each numbers in the sequence.

Example 1 of range() function

for i in range(5):
   print(i)

Output :-

0
1
2
3
4

Example 2 of range() function

for i in range(2,9):
   print(i)

Output :-

2
3
4
5
6
7
8

Example 3 of range() function using step parameter

for i in range(2,9,2):
   print(i)

Output :-

2
4
6
8

Example 4 of range() function

for i in range(0,-10,-2):
   print(i)

Run Code

Output :-

0
-2
-4
-6
-8

For Loop With Else In Python

The else is an optional block that can be used with for loop.The else block with for loop executed only if for loops terminates normally.This means that the loop did not encounter any break.

Example of For Loop With Else

list=[2,3,4,6,7]
for i in range(0,len(list)):
   if(list[i]==5):
      print(‘list has 5’)
      break
else:
   print(‘list does not have 5’)

Run Code

Output :-

list does not have 5