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’)

Advertisements

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 Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: