It is possible to specify that statements are to be repeated. Say that you would like to print a hello message to a list of persons. You could specify:
print "Hi Isabel, what a nice day isn't it?"
print "Hi Kate, what a nice day isn't it?"
print "Hi Michael, what a nice day isn't it?"
A nice feature is the for
control statement, that enables to specify that a statement (or a more complex sequence of statements) is to be repeated over a set of values:
for person in ['Isabel', 'Kate', 'Michael']:
print "Hi ", person
print ", what a nice day isn't it?"
Each value of the set is assigned one after the other to a variable, here the person
variable. Such a construct is generally called a loop
, and the variable person
is therefore called the loop variable.
Notice the indented block of statements below
. This block corresponds to the statements
that will be repeated over the set of values.