
Specific Statements
|
41
Extended print form
The print statement can also name an open output file-like
object to be the target of the printed text (instead of
sys.
stdout
):
fileobj = open('log', 'a')
print >> fileobj, "Warning-bad spam!"
If the file object is None, sys.stdout is used. Because sys.
stdout
can be reassigned, the >> form is not strictly needed;
however, it can often avoid both explicit
write method calls
and saving and restoring the original
sys.stdout value
around a redirected
print.
The if Statement
if test:
suite
[elif test:
suite]*
[else:
suite]
The if statement selects from among one or more actions
(statement blocks), and it runs the suite associated with the
first
if or elif test that is true, or the else suite if all are
false.
The while Statement
while test:
suite
[else:
suite]
The while loop is a general loop that keeps running the first
suite while the test at the top is true. It runs the
else suite if
the loop exits without hitting a
break statement.