0% found this document useful (0 votes)
9 views

PythonCodeStandard.v1.1

This document outlines the Python code standards for submissions, including rules on capitalization, variable conventions, input methods, line length, spacing, indentation, comments, program headings, and numerical methods. It emphasizes the importance of maintaining a consistent coding style to enhance readability and facilitate evaluation. Exceptions and additional guidance are provided for specific scenarios, encouraging good programming habits.

Uploaded by

basezero22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

PythonCodeStandard.v1.1

This document outlines the Python code standards for submissions, including rules on capitalization, variable conventions, input methods, line length, spacing, indentation, comments, program headings, and numerical methods. It emphasizes the importance of maintaining a consistent coding style to enhance readability and facilitate evaluation. Exceptions and additional guidance are provided for specific scenarios, encouraging good programming habits.

Uploaded by

basezero22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Code Standard

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

Bad Examples: DistanceIHaveToTravelToGetToNewYorkViaLondon


X
1) Exceptions may be made to this rule for loop and matrix indexes. For one
Letter variable names, the variable must be uppercase.
Examples: I
J
2) Variables may be up to 31 characters in length.

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.

Last Document Update: 01.2019 1


4 Line Length
A. Lines must not exceed 80 characters in length (remember that the “enter” key inserts 1
character that doesn’t appear on the screen). Commands requiring more than 80 characters
must be split across multiple lines. The backslash (\) is used as the continuation symbol in
free source formats.
B. One command per line.

5 Spacing and Parentheses


A. One space must be placed between arithmetic operators and their operands.

Examples: NewTemperature = InitialTemperature * 50.0


MyGrade = 70.0 + ExamScore

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: if ((ExamScore>0.0) and (ExamScore<=100.0)):

C. One space must be placed after commas and semicolons.

Example: foo(2, 1, 2)

D. Parameter lists may be preceded by an optional space.

Examples: def: foo(A, B, C)


def: foo (A, B, C)

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.

Examples: for I in range (0, 5):


Sum = Sum + I

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.

Last Document Update: 01.2019 2


Bad Example: # Variable to store temperature values
Temperature = 10
Good Example: # Initial temperature in degrees Celsius
Temperature = 10

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

# Increase the total grade and print the student's


# score
Grade = Grade + I
print('Total Grade = ', Grade)

C. There should always be a blank line above a line of comment.

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.

8 Program Heading information


A. Each file that you create and submit must contain a heading as the first several lines of
code, formatted as comments. The header must follow the following form:

For an INDIVIDUAL assignment:

# Assignment XX: An introduction to pretty Python code


# File: filename.m
# Date: 1 September 4-digit year
# By: Full Name
# UCusername
# Section: Number
# Team: Team Number
#
# ELECTRONIC SIGNATURE
# Full Name
#
# The electronic signature above indicates the script
# submitted for evaluation is my individual work, and I
# have a general understanding of all aspects of its
# development and execution.
#
# A BRIEF DESCRIPTION OF WHAT THE SCRIPT OR FUNCTION DOES
#

Last Document Update: 01.2019 3


For a Team assignment:
# Assignment XX : An introduction to pretty MATLAB code
# File: filename.m
# Date: 1 September 4-digit year
# By: Full Name team member 1
# UCusername
# Full Name team member 2
# UCusername
# Full Name team member 3
# UCusername
# Full Name team member 4
# UCusername
# Section: Number
# Team: Team Number
#
# ELECTRONIC SIGNATURE
# Full Name team member 1
# Full Name team member 2
# Full Name team member 3
# Full Name team member 4
#
# The electronic signatures above indicate the script
# submitted for evaluation is the combined effort of all
# team members and that each member of the team was an
# equal participant in its creation. In addition, each
# member of the team has a general understanding of all
# aspect of the script development and execution.
#
# A BRIEF DESCRIPTION OF WHAT THE SCRIPT OR FUNCTION DOES
#

B. Any “imports” should appear as the first executable line(s) of the script.

import sys
import math

Last Document Update: 01.2019 4


9 Numerical Methods
A. Mixed-Mode math should be avoided whenever possible. This includes the use of constants
in equations. For equations that are based on floating-point mathematics, floating-point
constants should be used. For equations that are based on integer mathematics, integer
constants should be used. One notable exception is to have an integer exponent for a
floating-point base, e.g. (0.52)

10 The Loophole for absent-minded GTAs, PTAs and Professors.


A. We understand that swallowing a full code standard at one time may be a bit much.
However, being able to write code that is neatly organized and uniform will go a long way to
your development as a problem solver, as well as programmer. A consistent (and logical)
style greatly improves the readability of your code. This makes it easier for us to quickly help
you, grade your projects, and award partial credit. You will also find that a consistent style
will help *you* to find errors in your code.

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.

Last Document Update: 01.2019 5

You might also like