Next: Truth, Falsehood and Logical
Up: Programming
Previous: Assignments
  Contents
Indentation
Unlike most programming languages, where indentation is used simply to improve
readability of programs, python uses indentation to signal the beginning and
end of blocks of statements, that is, groups of statements which will be executed
together based on the value of some condition, or the occurrence of an exception.
The first statement of your programs must start in the very first column, and
within each indented block, you must be consistent in the amount of indentation you use.
Although not strictly required, most python programmers try to be consistent in their
indentation from block to block as well. The task of writing properly indented
programs is made much easier if you use an editor which is python-aware; emacs
and vim are examples of such editors, as well as the IDLE
or pythonwin programming
environments supplied as part of python distributions.
Although it takes some getting used to, using identation to group statements together
has some definite benefits. Since there are no brackets or keywords involved in
delineating blocks of statements, no decisions need to be made as to whether the
delineators should appear on the same line as program statements, or if they should
line up with the beginnings or ends of blocks. The result of this is that most
python programs look very similar, even if they were written by different people, making
``foreign'' python programs much easier to read and understand. In addition, since
the indentation of a program actually determines its structure, python programs
provide easy-to-see visual clues to help you understand what a program is doing.
Next: Truth, Falsehood and Logical
Up: Programming
Previous: Assignments
  Contents
Phil Spector
2003-11-12