Dr Lucy Whalley / l.whalley@northumbria.ac.uk.
Course website: lucydot.github.io/python_novice
...but why is programming important?
For the findings of a study to be reproducible means that results obtained by an experiment or an observational study or in a statistical analysis of a data set should be achieved again with a high degree of reliability when the study is replicated.
Computational physics encompasses areas including materials modelling, particle physics simulations, protein structure prediction and plasma modelling. In fact, it is possible to find a computational branch for every major field in physics.
In programming, the little things matter! An extra full-stop or the smallest typo will stop your code running
Betty Snyder and I, from the beginning, were a pair. And I believe that the best programs and designs are done by pairs, because you can criticise each other, and find each others errors, and use the best ideas. Jean Bartik, Electronic Numerical Integrator and Computer (developed in 1945)
.py
extensionjupyter notebook
jupyter notebook
.ipynb
extensionUse your Jupyter notebook to...
Can you predict what the final value of position
is for the code block below?
initial = 'left'
position = initial
initial = 'right'
Data type | Python name | Definition | Example |
---|---|---|---|
integer | int | positive or negative whole numbers | -256 |
float | float | real number | -3.16436 |
string | str | character string | "20 pence." |
list | list | a sequence of values | ['frog',2,8] |
+ boolean, dict, tuple, complex, None, set
Which of the following will print 2.0?
first = 1.0
second = "1"
third = "1.1"
first + float(second)
float(second) + float(third)
first + int(third)
first + int(float(third))
int(first) + int(float(third))
2.0 * second
Data type | Python name | Definition | Example |
---|---|---|---|
integer | int | positive or negative whole numbers | -256 |
float | float | real number | -3.16436 |
string | str | character string | "20 pence." |
list | list | a sequence of values | ['frog',2,8] |
print()
, execution order len()
, string operations/indexing/slicing, type conversion: int()
, str()
, float()
min()
, max()
, round()
, help()
, runtime errors (exceptions), syntax errors print()
, execution order len()
, string operations/indexing/slicing, type conversion: int()
, str()
, float()
min()
, max()
, round()
, help()
, runtime errors (exceptions), syntax errors I want to sum the integers from 1 to 10. What is wrong with this code? How can I fix it?
total = 0
for number in range(10)
total = total + number
print(total)
mass = 4.2
if mass > 3:
print(mass, ' is large')
if mass < 2:
print(mass, ' is small')
if 2 <= mass <= 3:
print(mass, ' is just right')
What is wrong with the code? Fix the code so that it works as intended
grade = 95
if grade >= 70:
print("grade is C")
elif grade >= 80:
print("grade is B")
elif grade >= 90:
print("grade is A")
def print_greeting():
print ("Hello!")
def print_personalised_greeting(name):
print ("Hello "+name)
Fill in the blanks to create a function that takes a list of numbers as an argument and returns the first negative value in the list
def first_negative(values):
for v in ____:
if ____:
return ____
pressure = 103.9
def adjust(temperature):
new_temperature = temperature*1.43/pressure
pressure = 103.9
def adjust(temperature):
new_temperature = temperature*1.43/pressure
return new_temperature
print()
, execution order len()
, string operations/indexing/slicing, type conversion: int()
, str()
, float()
min()
, max()
, round()
, help()
, runtime errors (exceptions), syntax errors Document your code with docstrings
def calc_bulk_density(mass,volume):
density = mass / volume
return density
Document your code with docstrings
def calc_bulk_density(mass,volume):
"Return dry bulk density = powder mass / powder volume."
density = mass / volume
return density
What are the other two types of code documentation you can use?
Test your code
def calc_bulk_density(mass,volume):
"Return dry bulk density = powder mass / powder volume."
assert mass > 0, "mass must be more than zero"
assert volume > 0, "volume must be more than zero"
density = mass / volume
return density
Focus on readability
spam(ham[1], {eggs: 2})
spam( ham[ 1 ], { eggs: 2} )
x
, p
etc and expect the reader to know what they mean!)Think about reproducibility
Reproducibility is more complex and difficult than you might think..
One straight-forward thing you can do is print the version number for each package you import using print(packagename.__version__)
You want to select a random character from a string. base = "ATCHAGHRASG"
Feel free to look online (search for "Python standard library")
Crack the code using the following array which has the variable name Cipher
:
Cipher[0,1],Cipher[1,0],Cipher[2,1]*2,Cipher[1,1]
Cipher[0,2],Cipher[1,1],Cipher[2,0],Cipher[2,1],Cipher[1,2])
numpy.__version__
, variable names, assert statementsimport matplotlib.pyplot as plt
numpy.loadtxt()
, ndarray, numpy.savetxt
mean
, min
, max
plot()
, xlabel()
, ylabel()
, savefig()
This is just the beginning
You will meet Python again in future years (including KD5081).
You could also...