Python File handling

Python For Loop

Looping statement is used to execute a group of statement multiple times based on some condition.Python supports two loops.

Python For Loop

Python for loop is used when we want to execute some set of statement for each element present in sequences like string, list, tuple, set, dictionary, range and also allow to iterate iterable objects.

The for loop body statement repeat until sequence contain items. At every iteration pointer move to next item in sequence.

Syntax

for value in sequence:
    for-loop-body
#end of for loop

In above syntax for and in are keywords of Python.The sequence is an argument that we want to iterate and value contains current item from the sequence

Python for loop

In this image show the flow of for loop using flow chart

Examples

In this example, iterate list items using for loop

lst=[10,20,30]
for num in lst:
    print(num)
#end of for loop
10
20
30

In this output, we observe that the loop body execute till list have an item.If list reaches to last item, then loop gets terminated. At every iteration pointer move to next item.

In this example, we try to iterate character of string

name="Mcaprogramming"
for c in name:
    print(c)
#end of for loop
M
c
a
p
r
o
g
r
a
m
m
i
n
g

In this output, we observe that the loop body execute till pointer not reach to last character.

Python for loop with range function

The range() function is used to generate sequence of number.This help to implement for loop like other language.

Examples

In this example print 1 to 5 numbers.

for i in range(1,6):
    print(i)
#end of for loop

Output

1
2
3
4
5

In this output, range(1,6) generate a sequence of number 1 to 5 and that sequence iterate using for loop.This is same as for(i=1;i<6;i++).

In this example print even number betweeen 1 to 10

for i in range(1,11,2):
        print(i)
#end of for loop

Output

2
4
6
8

In this output, range(1,11,2) generate a sequence of number 1 to 10 with step 2 and that sequence iterate using for loop.This is same as for(i=1;i<11;i=i+2).

Python nested for loop

The Nested for loop means for loop inside the another for loop body.

Syntax

for value in sequence:
    outer for loop body
    for value in condition:
        inner-for-loop-body
    #end of inner for loop
    outer for loop body
#end of outer for loop 

Example

In this example print some pattern

n=int ( input( "Enter Number : ") )
for i in range(1,n+1):
    for j in range(1,i+1):
        print("*" , end = " ");
    #end of inner for loop
    print()
#end of outer for loop

Output

Enter Number : 3
* 
* * 
* * * 

Python for loop with else

The else block with for loop will execute when the all the sequence item processed and no more item present in the sequence.

If the loop terminates using break, then else block will not execute.

Syntax

for value in sequence:
    for loop body;
else:
    else body
#end of for loop

Example

In this example we search value in list given by the user

lst=[10,20,30,40]
s=int( input ( "Enter Number that you want to search in list : " ))
for value in lst:
    if value == s:
        print(s," present in list ");
        break;
else:
    print(s," not present in list ");
#end of for loop

Output

Enter Number that you want to search in list : 10
10 present in list

In this scenario value is present in list so that’s why, if condition inside the loop is becoming true and break the loop. So that’s why else block will not execute

Enter Number that you want to search in list : 25
25 not present in list

In this scenario value is not present in list so that’s why all sequence value is iterated and at the last no item is present in sequence so control moves to else block and execute the statements of else block and then terminated loop

1 thought on “Python For Loop”

  1. Pingback: Python Break Continue And Pass | MCAProgramming

Leave a Comment

Your email address will not be published. Required fields are marked *