PythonCodeStandard.v1.1
PythonCodeStandard.v1.1
Revision 1.1
This code standard is applicable for ALL Python code being submitted for evaluation, which
includes Exams, Homework, and Checks-For-Understanding (CFU’s). However, for Exams and
Checks-For-Understanding (CFU’s), you should apply Section 7 only if time permits.
1 Capitalization
A. All commands, functions, and reserved words should be all lowercase.
B. All variables should begin with a capital letter (see Variable Conventions).
C. All main program names, user-defined functions should follow the capitalization rules for
variables.
2 Variable Conventions
A. The first character of each word making up the name of a variable must be capitalized. The
remaining characters must be lowercase. If a variable name contains more than one word,
the words may be separated by an underscore.
Examples: Speed
RowNumber
My_Age
.
B. Variables should be descriptive, imaginative and meaningful, but not excessive in length.
Examples: FirstScore
Max_Score
3 Variable Input
A. Input should be done using the specific way it is indicated in each assignment (i.e. input(),
sys.stdin.read(), etc.). If input method is not specified in the assignment, use whichever way
is more convenient. You should pay attention to the order in which inputs are taken and
whether inputs are all in one line vs. multiple lines.
B. Input should always be preceded by a message indicating the type of input or some form of
instructions.
B. Logical operators need not have a space between the operator and their operands;
however, compound logical expressions should have each sub-expression parenthesized.
Example: foo(2, 1, 2)
E. The tab key is NOT to be used, as there is no uniform standard definition of what the tab
key does. Instead, use four spaces as stated above.
6 Indentation
A. Exactly four spaces must be used to indent blocks.
B. Indentations are not to be used anywhere in the code except for specifically indented
blocks.
7 Comments
A. Comment lines should be used frequently to explain the function of given blocks of code.
They should explain the intention of the code, not the obvious. A good general rule is that if
the code was not immediately obvious to you, then you should provide some form of
comment. This is to aid you when you go back and look at your programs several years in
the future (Believe me, you will!). Points will be taken off homework assignments if
reasonable comments are not in the code.
B. If the comments are lengthy, they should be placed above the block of code that they
explain and should be indented with the code.
Example:
# If the score is valid
if ((I>=0) and (I<=100)) then
D. For comments longer than one or two lines that pertain to program development,
operation, error testing, output interpretation, etc., there should be a blank line above and
below the comment block.
B. Any “imports” should appear as the first executable line(s) of the script.
import sys
import math
We recognize that we have not addressed every possible situation. Our intent was to
provide you a framework to aid you in developing good programming habits. If you find
something that we did not cover in this standard, use your best judgment, or ask a GTA or
PTA.