BIOS1100 H17 uke 1
week 1
Ukens læringsmål
- bli kjent med kursets oppsett og ressurser
- kunne jobbe med Jupyter Notebook
- kunne lage og kjøre et enkelt python program
- forstå og kunne bruke noen programmeringselementer
- variabler
- tall og tekst
-
import
av koden utenfor notebook - bruk av funksjoner i python
- kunne bruke python for enkle vitenskapelige beregninger
Hvorfor programmering
- datamaskinen er nå essensiell i biologien
- målet er altid å få svar på et biologisk spørsmål
- få dette svaret
- fortere
- mer nøyaktig
- etterprøvbar
- repeterbar
Hvorfor programmering
- biologer trenger å kunne lage programmer selv
- biologer trenger å kunne vurdere resultater kritisk
Hvorfor Python
- brukt mye i vitenskapen og biologien
- attraktiv syntaks
- letter å lære (bort) enn andre språk
Hva er et program
- instruksjoner som sier til datamaskinen hva den skal gjøre
- instruksjoner er linjer med tekst
- datamaskinen gjør nøyaktig det du ber dem om å gjøre
Programmering
- planlegge koden du trenger
- skrive koden
- kjøre koden og sjekke resultater
- korrigere koden (debugging)
Hvordan python
- Jupyter Notebook
DEMO av Jupyter Notebook
Ukens programmeringsbegreper
- variabler og type variabler
- tall og tekst
- regne i python (med og uten variabler)
-
import
av koden utenfor notebook - bruk av funksjoner i python
Hjertefrekvens
The target heart rate (HR
)
$$
\text{HR}_{\text{max}} = 220 - \text{age}
$$
Hjertefrekvens
The target heart rate for a specific intensity:
Multiply the maximum heart rate with the given intensity $$ \text{HR}_\text{target} = \text{HR}_\text{max} \times \text{intensity} = (220 - \text{age}) \times \text{intensity} $$
Fahrenheit to Celsius
If \( F \) is the temperature in Fahrenheit, and \( C \) is the temperature in Celsius, this conversion between them is made by the formula, $$ \begin{equation} C = (F-32)/1.8\;. \label{_auto1} \end{equation} $$
Mathematical functions
Some of the most common mathematical functions in the pylab
package:
Function name | description |
---|---|
sin(x) | the sine of \( x \), \( \sin(x) \) |
cos(x) | the cosine of \( x \), \( \cos(x) \) |
tan(x) | the tangent of \( x \), \( \tan(x) \) |
factorial(x) | the factorial of \( x \), \( x! \) |
exp(x) | \( e \) raised to the power of \( x \), \( e^x \) |
sqrt(x) | the square root of \( x \), \( \sqrt{x} \) |
log(x) | the natural (base \( e \)) logarithm of \( x \), \( \ln(x) \) |
log10(x) | the base 10 logarithm of \( x \), \( \log_{10}(x) \) |
pi | \( \pi \) to numerical precision, 3.14159... |
e | \( e \) to numerical precision, 2.71828... |
A simple temperature model
The average temperature, measured in degrees Celsius, during the course of a year in Oslo, the capital of Norway, is modeled fairly well by the expression $$ \begin{equation} T(x) = 6 + 10\sin\left({2\pi \over 365} x - 1.9\right), \label{_auto2} \end{equation} $$ where
- \( T \) is the average temperature
- \( x \) is time, measured in days
- \( x=1 \) is January 1st, and so on
Math and code
$$ \begin{equation} T(x) = 6 + 10\sin\left({2\pi \over 365} x - 1.9\right), \label{_auto3} \end{equation} $$ where
- \( T \) is the average temperature
- \( x \) is time, measured in days
- \( x=1 \) is January 1st, and so on
from pylab import *
x = 100 # day of the year
T = 6 + 10*sin(2*pi*x/365 - 1.9) # average temperature (in Celsius)
print("Average temperature on day", x, ":", T, "degrees Celsius")
Summary
Variables
- Data is stored in variables
- This is called assignment
- Variables make it possible for us to give the data a name, which can be used to access the data later in our program
- Good variable names make the code easier to read
Types of variables
- There are several types of variables in Python
- We have been working on the following types
- Floats
- Integers
- Strings
Functions
- A function is a piece of previously defined code
- Either by yourself or others that
- Functions perform specific tasks
- A function can take zero, one or multiple arguments as input (inside the parenthesis) and may return a value
- When in a Jupyter Notebook you can get information about any function by writing the name of the function followed by a question mark,
?
Order of operations
When performing multiple operations in a single line of code the order in which the operations are performed is the following:
- First, we calculate everything inside parentheses,
- then we do all function calls,
- then we do all powers,
- then we do all multiplications and divisions,
- finally, we do all additions and subtractions.
Comments
- The main point of comments is to make the program easier to read
- They begin with a hashtag and everything after this character is ignored when the program is run
Packages
- A package is a collection of functions for performing specific tasks that we can use
- A package that contains many functions we need for scientific calculations, is named
pylab