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.
|--------|---------|---------|---------|---------|---------|---------|---------
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)
|--------|---------|---------|---------|---------|---------|---------|---------
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))
|--------|---------|---------|---------|---------|---------|---------|---------
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.
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)
|--------|---------|---------|---------|---------|---------|---------|---------
In [ ]: