Roshan Book

My Tech Notebook

Python tutorial 9–iterators and controlling loops


getting through data, one by one and doing something with it

For loop is designed to work with iterators

for line in fh.readlines():

print(line, end=’’)

In python all the container types are iterators

Common iterators

‘string’ – container that containers individual characters

list

Enumerating Iterators

for index,line in enumerate(fh.readliness()):

if c—‘s’  : print(‘index{} iis an s’.format(i))

# Controlling the loop through break and continue

Break – terminate the loop and come out of it

Continue – break one cycle of loop and continue from next loop

usage

if c==’s’  : break/continues

# For else- code which runs once for loop is over. it also works as while else

for c in s:

            print (c,end=’’)

else:

            print (‘else’)

One response to “Python tutorial 9–iterators and controlling loops

  1. Delena Adolphson October 17, 2011 at 7:27 am

    nice work, continue the great blog.

Leave a comment