Remember there are "official" Python Style guidelines you can refer to here for more detail. This list is meant to be a quick reminder of specific issues we have seen in homeworks this quarter.
Students shouldn't use single letter names for variables that should really be 'factorial', 'current_sum', or 'reciprocal'. n, i, and j are fine for loop variables.
You should use descriptive variable names, such as "factorial" or "current_sum", that describe the value the variable contains.
In Python, it’s recommended to name variables and functions in a certain way so that other Python programmers can read your code more easily. Here are some examples of variable names: "num_lines", "triangular_number", "running_sum", "my_friends", etc. Note: Other programming languages (e.g. Java) have different conventions for naming. Use the conventions of the language you are working in.
It’s recommended that you put spaces around binary operators such as +, -, /, *, and **. This makes the code easier to read in most cases.
It’s recommended that you put spaces around comparisons such as <, <=, >, >=, and ==. This makes the code easier to read in most cases.
Do this:
if val > 120:print "Green!"elif val > 80:print "Blue!"elif val > 32:print "Red!"else:print "Orange!"
Instead of this:
if val > 120:print "Green!"else:if val > 80:print "Blue!"else:if val > 32:print "Red!"else:print "Orange!"
Do this:
for base in nucleotides:if base == 'A':# code hereelif base == 'C':# code hereelif base == 'T':# code hereelif base == 'G':# code here
Instead of this:
for base in nucleotides:if base == 'A':# code hereif base == 'C':# code hereif base == 'T':# code hereif base == 'G':# code here
Do this:
for base in nucleotides:if base == 'A':# code hereelif base == 'C':# code hereelif base == 'T':# code hereelif base == 'G':# code here
Instead of this:
for base in nucleotides:if base == 'A':# code herefor base in nucleotides:if base == 'C':# code herefor base in nucleotides:if base == 'T':# code herefor base in nucleotides:if base == 'G':# code here
Don't write the same code again if you already have a function that performs that action. Just call the function.
Perhaps two similar but not identical pieces of code can be turned into a function by using parameters.
Be sure to include a docstring for each function.
Include other comments (with #) inside of the function to describe what you are doing. Be sure to add comments to tricky parts of code. However do avoid unecessary comments (e.g. # increment count) which should be obvious from looking at the code.