Performing repetitive tasks
using the for loop
Sometimes you need to perform a task more than one time. You use the
for
loop
statement when you need to perform a task a specific number of times. The
for
loop has a definite beginning and a definite end. The number of times that this
loop executes depends on the number of elements in the variable you provide. To
see how this works, open a copy of IPython and type the following code:
def DisplayMulti(*VarArgs):
for Arg in VarArgs:
if Arg.upper() == 'CONT':
continue
print('Continue Argument: '
+ Arg)
elif Arg.upper() == 'BREAK':
break
print('Break Argument: '
+ Arg)
print('Good Argument: '
+ Arg)
In this case, the
for
loop attempts to process each element in
VarArgs
. Notice that
there is a nested
if
statement in the loop and it tests for two ending conditions.
84
PART 1
Do'stlaringiz bilan baham: |