Dr Lucy Whalley / l.whalley@northumbria.ac.uk.
Course website: lucydot.github.io/python_novice
Pre-course questionnaire: https://bit.ly/python-31Jan
Your thoughts?? 🤔
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.
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.
Tech and IT-related job vacancies now make up 12% of all open UK job vacancies
The data skills gap:
Almost half of businesses (48%) are recruiting for roles that require hard data skills but under half (46%) have struggled to recruit for these roles over the last 2 years.
The focus of this workshop is to equip with you with some of the transferable skills needed for success in a range of computational applications.
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
Dr Lucy Whalley / l.whalley@northumbria.ac.uk.
Course website: lucydot.github.io/python_novice
print()
, execution order len()
, string operations/indexing/slicing, type conversion: int()
, str()
, float()
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] |
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)
print()
, execution order len()
, string operations/indexing/slicing, type conversion: int()
, str()
, float()
min()
, max()
, round()
, help()
, runtime errors (exceptions), syntax errors 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 ____
Course website: https://lucydot.github.io/python_novice/
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
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__)
def triangle_area(base,height):
"""returns the area A of a triangle using the formula A=0.5*b*h where b is the base and h is the height."""
assert base > 0 and height > 0, "negative lengths are not allowed"
return 0.5*base*height
You want to select a random character from a string. base = "ATCHAGHRASG"
Feel free to look online (search for "Python standard library")
Decipher the message below using the following array which has the variable name Cipher
(note that you do not need to write any code to complete this exercise).
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 numpy
, import numpy as np
numpy.loadtxt()
, array attributes, numpy.savetxt()
, numpy.zeros
plt.plot
, plt.legend()
,plt.title()
, plt.xlabel()
, figures and sub-plotsCourse website: https://lucydot.github.io/python_novice/
print()
, execution order len()
, string operations/indexing/slicing, type conversion: int()
, str()
, float()
min()
, max()
, round()
, help()
, runtime errors (exceptions), syntax errors 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 could...