Iterator in Python : FOR loop , WHILE loop
Iteration is also called looping mechanism begins with a conditional statement like a test. If the test
evaluates to True then the program executes the loop body once and then goes back to reevaluate the test. This process is repeated until the test evaluates to False, after which control passes to the code following the iteration statement.
The simplest example of while loop on accesing the stack of files will be till we reach the last or the first file in stock , keep on printing the file name.
e.g.
number_of_files = total number of files in stock
If the in the test condition we give such a condition that the test is always true than it becomes an infinite loop.For eg. in the code section above if we give the condition number_of_files <= 0 which will be a true condition always , so on running the program it will keep on printing "more files left" unless stopped by a keyboard interrupt.
e.g
for i in range (0,5):
print i
i will iterate over elements of the range 0 to 5 excluding the end point i.e. 5 in this case. Output will be :
0
1
2
3
4
we can also specify the step with which the for loop can jump a step before ,reaching the end point.
For loop can also be used to access each letter of the string
It works like this :
{for all elements in list}
{do this}
for eg.
for c in characters:
print c
o/p wil print all the alphabets in 'characters'
Break
Break statement is used when we want to break out from the loop body.
Continue
Continue statement is used to tell Python to skip the rest of the statement in the current loop block and to continue to the next iteration of loop.
Nested loops
A nested loop is loop inside a loop
Example:
x=`3
for i in range(x):
for j in range (x):
print i , j
will give the o/p
0,0
0,1
0,2
1,0
1,1
1,2
2,0
2,1
2,2
that means for every iteration of the outer loop complete iteration of the inner loop will be done.
Iteration is also called looping mechanism begins with a conditional statement like a test. If the test
evaluates to True then the program executes the loop body once and then goes back to reevaluate the test. This process is repeated until the test evaluates to False, after which control passes to the code following the iteration statement.
While loop:
While loop takes a decremented function and runs until the condition remains true.The simplest example of while loop on accesing the stack of files will be till we reach the last or the first file in stock , keep on printing the file name.
e.g.
number_of_files = total number of files in stock
while number_of_files!=0:Infinite loop:
print "more files left"
number_of_files - = 1
If the in the test condition we give such a condition that the test is always true than it becomes an infinite loop.For eg. in the code section above if we give the condition number_of_files <= 0 which will be a true condition always , so on running the program it will keep on printing "more files left" unless stopped by a keyboard interrupt.
For Loop :
For loop executes over a given range of sequence. We can specify the starting and the end point in the range function given with for loope.g
for i in range (0,5):
print i
i will iterate over elements of the range 0 to 5 excluding the end point i.e. 5 in this case. Output will be :
0
1
2
3
4
we can also specify the step with which the for loop can jump a step before ,reaching the end point.
For loop can also be used to access each letter of the string
It works like this :
{for all elements in list}
{do this}
for eg.
for c in characters:
print c
o/p wil print all the alphabets in 'characters'
Break
Break statement is used when we want to break out from the loop body.
for i in range (0,10):
if i == 3:
break
print i
Continue
Continue statement is used to tell Python to skip the rest of the statement in the current loop block and to continue to the next iteration of loop.
for i in range (0,10):
if i == 3:
break
print i
Nested loops
A nested loop is loop inside a loop
Example:
x=`3
for i in range(x):
for j in range (x):
print i , j
will give the o/p
0,0
0,1
0,2
1,0
1,1
1,2
2,0
2,1
2,2
that means for every iteration of the outer loop complete iteration of the inner loop will be done.
No comments:
Post a Comment