2 Types of Looping in Python (for, while, nested loop)

Preview
You must first complete Basic operators in Python before viewing this Lesson

Looping statements in Python

Looping statements are used to repeat same set of statements again and again for a specific number of times or depending upon a condition.There are two types of looping statements Python:

  • for loop
  • while loop



1. for loop

for loop is basically used when we know how many times, a specific set of statements should be executed.  It is used to work with a range of values or traverse and manipulate each element of a sequence including list, tuple , dictionary, set or string.

The syntax of for statement is:

for <var> in <sequence>or range():

     Statements-Block1

else:

     Statements-Block2

for is a keyword which specifies that for statement has started

sequence may be a list, tuple, dictionary, string or set whose values are to be traversed

range() is a predefined Python function to generate a list of values on the basis of its arguments.

var refers to a variable which fetches values of a sequence one after another one at a time.

Statement-Block1 is the set of python statements which execute as a part of for statement. These statements will repeatedly execute until all values of sequence are traversed.

Statement-Block2 is the set of python statements which execute after the traversal of all elements of the sequence.

else block is executed only if for loops terminates normally.


a. Using for statement with List

for statement can be used to traverse each individual value of a list.

Program 1: To display all elements of a list using for statement.

Program

Output

for var in [10,20,30]:
     print(var)

10

20

30

In the above example, for var in [10,20,30] is used to traverse values in the list [10,20,30] one at a time.

  1. At first 10 goes to variable var, 10 gets printed.
  2. Then 20 goes to variable var, 20 gets printed.
  3. At last, 30 goes to variable var, 30 gets printed.



b. Using for statement with range() function

range() is a predefined function of Python to display a set of values. range() function can be used in following manner

Example

Values

Generated

Explanation
range(5)0,1,2,3,4

Values generated between 0 to 4.

5 not included.

range(1,5)1,2,3,4

Values generated between 1 to 4.

5 not included

 

range(1,10,2)1,3,5,7,9Values generated between 1 to 9 with gap of 2.
Range(10,0,-2)10,8,6,4,2

Values generated between 10 to 2.

-2 is the step size i.e there is a gap of -2 between values starting with 10 .

 

Program 2: To display values from 0 to 5  using for statement.

Program

Output

for var in range(6):

     print (var)

0
1
2
3
4
5

In the above program, statement for var in range(6) works as:

range(6) returns values 0,1,2,3,4 and 5.

At first 0 goes to var, it is shown using print(var) statement.

Then 1 goes to var, it is shown.

Then 2 goes to var, it is shown.

Then 3 goes to var, it is shown.

Then 4 goes to var, it is shown.

Then 5 goes to var, it is shown.

Program 3: To display characters contained in a string using for statement.

ProgramOutput
for var in “hello”:
     print(var)
h
e
l
l
o

In the above program, statement for var in “hello” picks one character at a time from the string value “hello” and shows all the characters one at a time.

Program 4: Display even elements of a list using for statement with else clause.

ProgramOutput
numbers=[1,3,12,9,4,7,21,8,11,6]
print(“Even elements are”)
for num in numbers:
     if num%2==0:
          print (num)
     else:
          print(“Bye”)
Even elements are
12
4
8
6
Bye

In the above program, even elements of the list are displayed.

Bye is displayed at the end due to else clause.



c. Nesting of for loop

Nesting of for means that one for loop is enclosed within another for loop (outer loop).

For each value of outer loop, inner loop gets completely executed.

Program 5: Program to demonstrate nesting of for statement.

ProgramOutput

for i in range(1, 4):

     for j in range(1, 4):       

          print(i,j)

1     1
1     2
1     3
2     1
2     2
2     3
3     1
3     2
3     3

In the above program,

outer for loop for i in range(1, 4) generates values from 1 to 3

inner for loop for j in range(1, 4) generates values from 1 to 3

for i=1, j  will generate values 1,2,3

So output will be:

1     1

1     2

1     3

for i=2, j  will generate values 1,2,3

So output will be:

2     1

2    2

2     3

Similarly for i=3, j  will generate values 1,2,3

So output will be:

3     1

3    2

3     3



 2. While statement

While is an entry controlled looping statement. It always works with a condition. All statements which are part of while statement repeatedly get executed as long as condition is true.

The syntax of while statement is:

initialization

while condition:

     Statement-Block

     updation

else:

     Statement-X

  while is a keyword which specifies that while statement has started

 Initialization is the way to store some initial value in the control variable. This variable can be of any data type. It is used to specify the value from which the loop should start.

Example : i=1

 Condition is relational or logical expression. If condition is true, the loop will execute. The loop will continuously execute as long as condition is true. Loop will terminate when the condition becomes false.

In this part, control variable is generally tested to check whether the loop should continue or not. If condition is initially false, the loop will not execute even once.

Example:   i<=10

Statement Block is group of Python statements which will execute as a part of while statement. Statement block can contain one or more statements.

Updation is the way to increase or decrease the value of a control variable..

Example: i+=2

else block is executed only if for loops terminates normally.

Statement-X refers to one or more Python statements which will get executed when while statement terminates normally.

While statement can be explained through the following flowchart:

Program 6: Program to display values from 1 to 5 by using while statement.

ProgramOutput
i=1
while i<=5:
     print (i)
     i+=1

2

3

4

5

Above program works in the following manner

Initially i=1,

Pointer jumps to condition

  

condition i<=5 is true

i.e. i=1 is less than 5

 print(i) will  display,

1

i+=1 increments the values of i by 1,

i =2

condition i<=5 is again true

i.e i=2 is less than 5

So print(i) will  show

2

i+=1 makes

i =3.

condition  i<=5 is again true

i.e i=3 is less than 5

So print(i) will  show

3

i+=1 makes

i =4

condition i<=5 is again true

i.e i=4 is less than 5

So print(i) will  show

4

i+=1 makes

i =5

condition i<=5 is true

i.e i=5 is equal to  5

So print(i) will  show

5

i+=1 makes

i =5

i<=5 is false

i.e. 6 not less than 5. Hence while loop terminates

 

Program 7: Program to display values from 10 to 5 by using while statement.

ProgramOutput
i=10
while i>=5:
print (i)
i=i-1
10
9
8
7
6
5



Nesting of while loop

Nesting of while loop means that one while loop is enclosed within another while loop (outer loop).

For each value of outer loop, inner loop gets completely executed.

Program 8: Program to demonstrate nesting of while statement.

ProgramOutput
i=1
while i<=3:
     j=1
     while j<=3:
          print (i,j)
          j+=1
     i+=1
1     1
1     2
1     3
2     1
2     2
2     3
3     1
3     2
3     3

In the above program,

outer while loop while i<=3 works for values 1 to 3

inner while  loop while  j<=3  also generates values from 1 to 3

for i=1, inner while loop will  generate values 1,2,3 of j

So output will be:

1     1

1     2

1     3

for i=2, inner while loop will  generate values 1,2,3 of j

So output will be:

2     1

2    2

2     3

Similarly for i=3, j  will generate values values 1,2,3  of j

So output will be:

3     1

3    2

3     3

Lesson tags: for loop in python, nesting of for loop in python, nesting of loop in python, nesting of while loop in python, range() function in python, range() in python, types of loop in python, while loop in python
Back to: Python Programming Tutorial