Roshan Book

My Tech Notebook

Tag Archives: while loop

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

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

%d bloggers like this: