The Astro 300 Python programming style guide

This notebook is a summary of the python programming style we will use in Astro 300.

Half of your grade, on each assignment, will be based on how well your code follows these guidelines.

These guidelines are a small subset of the PEP 8 programming style used by python developers.

|--------|---------|---------|---------|---------|---------|---------|---------

Variable Names

  • use only lowercase letters [a-z] and underscores [ _ ]
  • no blank spaces between the characters
  • avoid using a single character as a variable name
  • The purpose of the variable should be obvious from its name

An Example:

You want to find the kinetic energy of a particle with mass 10 and velocity 20:

$$ \mathrm{Kinetic\ Energy}\ = \frac{1}{2}\ mv^2 $$


In [ ]:
# Good - Full credit

mass_particle = 10.0
velocity_particle = 20.0

kinetic_energy = 0.5 * mass_particle * (velocity_particle ** 2)

print(kinetic_energy)

In [ ]:
# Bad - Half credit at best

x = 10.0
y = 20.0

print(0.5*x*y**2)

In [ ]:
# Really bad - no credit

print(0.5*10*20**2)

|--------|---------|---------|---------|---------|---------|---------|---------

Function Names

Use the same guidelines as for variable names


In [ ]:
# Good

def find_kinetic_energy(mass_part, velocity_part):
    
    kinetic_energy = 0.5 * mass_part * velocity_part ** 2
    return(kinetic_energy)

In [ ]:
# Bad

def KE(x,y):
    return(0.5*x*y**2)

In [ ]:
# Good

mass_particle = 10.0
velocity_particle = 20.0

kinetic_energy = find_kinetic_energy(mass_particle,velocity_particle)

print(kinetic_energy)

In [ ]:
# Bad

print(KE(10,20))

|--------|---------|---------|---------|---------|---------|---------|---------

Line Length

  • Limit all lines to a maximum of 79 characters
  • The piece that seperates the sections in this notebook is 79 characters long
  • If you have to scroll horizontally - your line is too long

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses (), brackets [] and braces {}.

Long lines can be broken over multiple lines by wrapping expressions in parentheses.

An Example:


In [ ]:
# Some variables to use in an equation

gross_wages = 50000
taxable_interest = 1000
dividends = 50
qualified_dividends = 10
ira_deduction = 2000
student_loan_interest = 3000
medical_deduction = 1000

In [ ]:
# Good

income = (gross_wages
          + taxable_interest
          + (dividends - qualified_dividends)
          - ira_deduction
          - student_loan_interest
          - medical_deduction)

print(income)

In [ ]:
# Bad
 
income = gross_wages + taxable_interest + (dividends - qualified_dividends) - ira_deduction - student_loan_interest - medical_deduction

print(income)

Long text can be wrapped in """


In [ ]:
quote_the_raven = """
Once upon a midnight dreary, while I pondered, weak and weary,
Over many a quaint and curious volume of forgotten lore—
While I nodded, nearly napping, suddenly there came a tapping,
As of some one gently rapping, rapping at my chamber door.
"Tis some visiter," I muttered, "tapping at my chamber door—
            Only this and nothing more."
"""

In [ ]:
print(quote_the_raven)

|--------|---------|---------|---------|---------|---------|---------|---------

Spaces

  • Use spaces around math symbols

Yes: my_answer = 2 + 4

No: my_answer=2+4

  • Avoid spaces inside parentheses, brackets or braces, except after commas.

Yes: my_answer = spam(ham[1], {eggs: 2})

No: my_answer = spam( ham[ 1 ], { eggs: 2 } )

You should always try to make your code as readable as possible.

Feel free to break any of these rules if it makes your code easier to understand by someone who has never seen your code.