Python Formatting Guidelines
Why have guidelines?
A style guide provides provides a map so that the code generated by a group of programmers will be consistent and, therefore, easier to read and maintain. †
It is also an essential part of joining a community, and as with most communities the rules vary. In the case of programming they vary across languages, and even across major projects or companies using a language, e.g.
C++: GeoSoft's, Google's, Java, PHP, C#, Perl, Ruby, and of course, Python.
CPSC 128 Python Guidelines
These are extracted from the Python guidelines above, so you will not have to wade through too many language features you won't recognize.
-
Use four spaces for indentation. (IDLE does this for you. If you're using another editor, set it to use four space characters, not a tab character).
-
Limit lines to 80 characters.
-
Separate logical sections of a program with single blank lines.
-
Begin every program with an opening comment block containing at least:
# filename # Author # Date
-
Place import statements right after the opening comment block.
-
Initialize variables as near their first use as possible.
-
Do not leave a space between
input
and its opening(
. -
Do not leave a space after ( or before ).
-
Place a single space on each side of the binary operators
=
,==
,<
,<=
,!=
,<>
,>=
,>
,and
,or
,not
, andin
. Use spaces around arithmetic operators too, though in long expressions using grouping for readability takes precedence. -
Comments should be complete sentences.
-
Comments should add to the code not repeat it, and should never state the obvious.
-
Variable names should be lower_case.
-
Constant names should be UPPER_CASE.