Now that we have introduced compound statements, you may need to
make an adjustment to your editor. Set your editor to use spaces instead
of tabs. Most Python is typed using four spaces instead of the ASCII tab
character (^I). Most editors can be set so that when you hit the
Tab
key on your keyboard, the editor inserts four spaces.
IDLE is set up this way by default. A good
editor will follow the indents so that once you indent, the next line is
automatically indented.
We'll show the spaces explicitly as ␣'s in the following
fragment.
if␣a␣>=␣b:
␣␣␣␣m␣=␣a
if␣b␣>=␣a:
␣␣␣␣m␣=␣b
This is has typical spacing for a piece of Python
programming.
Note that the colon (:) immediately follows the condition. This is
the usual format, and is consistent with the way natural languages (like
English) are formatted.
These
if
statements can be collapsed to
one-liners, in which case they would look like this:
if␣a␣>=␣b:␣m␣=␣a
if␣b␣>=␣a:␣m␣=␣b
It helps to limit your lines to 80 positions or less. You may need
to break long statements with a \
at the end of a line. Also,
parenthesized expressions can be continued onto the next line without a \.
Some programmers will put in extra ()'s just to make line breaks
neat.
While spaces are used sparingly, they are always used to set off
comparison operators and boolean operators. Other mathmatical operators
may or may not be set off with spaces. This makes the comparisons stand
out in an
if
statement or
while
statement.
if␣b**2-4*a*c␣<␣0:
␣␣␣␣print␣"no␣root"
This shows the space around the comparison, but not the other
arithmetic operators.