-
Simple statements must be complete on a single Logical Line.
Starting in Chapter 7, Truth, Comparison and Conditional
Processing
we'll look at compound
statements, which have indented suites of statements, and which span
multiple Logical Lines. The rest of these rules will define how
Logical Lines are built from Physical Lines through a few Line Joining
rules.
-
Physical Lines are defined by the platform; they'll end in
standard \n
or the Windows ASCII CR
LF
sequence (\r\n
).
-
Comments start with the #
character outside a
quoted string; comments end at the end of the physical line. These are
not part of a statement; they may occur on a line by themselves or at
the end of a statement.
-
Coding-Scheme Comments. Special comments that are by VIM or
EMACS can be included in the first or second line of a Python file.
For example, # -*- coding: latin1 -*-
-
Explicit Line Joining. A \
at the end of a physical
line joins it to the next physical line to make a logical line. This
escapes
the usual meaning of the line end
sequence. The two or three-character sequences (\\n
or
\\r\n
) are treated as a single space.
-
Implicit Line Joining. Expressions with ()
's,
[]
's or {}
's can be split into multiple
physical lines.
-
Blank Lines. When entering statements interactively, an extra
blank line is treated as the end of an indented block in a compound
statement. Otherwise, blank lines have no signficance.
-
Indentation. The embedded suite of statements in a compound
statement must be indented by a consistent number of spaces or tabs.
When entering statements interactively or in an editor that knows
Python syntax (like IDLE), the indentation
will happen automatically; you will outdent by typing a single
backspace
. When using another text editor, you will
be most successful if you configure your editor to use four spaces in
place of a tab. This gives your programs a consisent look and makes
them portable among a wide variety of editors.
-
Whitespace at the beginning of a line is part of indentation,
and is significant. Whitespace elsewhere within a line is not
significant. Feel free to space things out so that they read more like
English and less like computer-ese.