BIOS1100 H17 uke 2
Ukens sitat
'Hvis du er på MatNat så kommer du til å trenge å programmere - mitt beste (fag)tips er Jobb med programmeringer - du er ikke ferdig med programmering selv om du "bare" har ett obligatorisk fag der det står programmering.'
Sunniva Rose, 23. august 2017 sunnivarose.no
Ukens forelesning
- noen praktiske ting
- begrepsforståelse kapittel 1
- utvalgte øvelser
- nytt stoff uke2
Læringsmål uke 1
- 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
En del praktiske ting
- Beskjeder
- Piazza
- Devilry
- Juyterhub
- Løsningsforslag til ukens legges ut hver uke etter siste gruppeundervisning (senest mandag morgen)
Begrepsforståelse kapittel 1
Mentimeter
- tenk
- par
- stem
Exercise 1: Addition
What is the output of the following code:
str_1 = "1"
str_2 = "3"
print(str_2 + str_1)
13
4.0
31
4
Answer. 3.
Solution. In this case we have added two string variables together. The result of adding "3" and "1" will be 31
. If str_1
and str_2
were added in different order (str_1 + str_2)
, the result would be 13
.
Exercise 2: Calculating with variables
What is the output of the following code:
a = 5.0
b = 3.0
c = a + b
a = 3.0
d = 2.0
e = a + c * d
print(e)
18.0
15.0
22.0
19.0
Answer. 4.
Solution. c = 5.0 + 3.0 = 8.0
Then we change the value of variable a
to 3.0
which gives: e = 3.0 + 8.0 * 2.0 = 19.0
Variable c
is left unchanged since variable a
was not changed before c
was defined.
Another thing you need to pay attention to is the order of mathematical operations. In Python multiplication is ranked higher than addition, just like in mathematics.
Exercise 3: Type conversion (1)
a) Given this code:
first = 1.0
second = "1"
third = "1.1"
Which of the following will print 1.0
?
int(third)
int(first)
float(second)
1.0 * second
Answer. Option 3
Solution.
- Gives an error: one cannot use
int()
with a string that containing a number of type float int()
converts to an integer, which does not have any digits- Correct. even though the variable contains an integer (as a sting), the
float()
function will convert it to a float - Since the second is a
str
this also results 'mixingint
andstr
' error
Exercise 4: Type conversion (2)
a) Given this code:
first = 1.0
second = "1"
third = "1.1"
Which of the following will print 2.0
?
first + int(third)
first + int(float(third))
int(first) + int(float(third))
2.0 * second
Answer. Option 2
Solution. Note that the answer is a number of type float.
- This gives an error, as
int
cannot convert a text containing a number of type float float(third)
converts "1.1" to a number of type float, and theint()
function converts it to1
. Soint(float(third))
results in an int. However, sincefirst
is a float, the final result will also be a float- Both
int(first)
andint(float(third))
results in int's, so the final result will also be an int - this results in an error:
second
is of type str, and this cannot be multiplied with a variable that is not of type str
Exercise 5: Error message
a) What kind of error message does this code give?
conc1 = 6
v1 = 10
v2 = 4
conc2 = (conc1*v1)/v2
print("The final concentration is: " + conc2)
ValueError
TypeError
SyntaxError
NameError
Answer. Option 2. is correct.
Solution.
TypeError Traceback (most recent call last)
TypeError: Can't convert 'float' object to str implicitly
Exercise 6: Rounding off
a) Description
my_number = 123.54
Which command will print 123
?
round(my_number,0)
int(my_number)
int(round(my_number,0))
str(my_number)
Answer. The 2nd answer is correct.
Solution.
- The number will be rounded up, is still a float and this will print
124.0
- Correct. NOTE that the number is not rounded up!
- The number will first be rounded up and this will thus print
124
str()
does not do any rounding and this will print'123.54'
Take home message: take care while converting floats to ints!
Exercise 7: Maximum
a) What will this do?
my_list = [1, 'a']
print(max(my_list))
- prints
1
- prints
a
- gives
SyntaxError
- gives
TypeError
Answer. TypeError: unorderable types: str() > int()
Solution. Solution (code)
# python code
Utvalgte øvelser
- Exercise 12: heating water (and camels)
Læringsmål uke 2
Biologi
- kunne forklare hvordan bakterier formerer seg
- kunne de forkjellige faser i bakteriell vekst
Programmering
- kunne visualisere et enkelt datasett
- kunne lagre visualiseringen i en fil
- kunne last inn et enkelt datasett fra fil
- kunne bruke lister i python
Matematikk
- kunne beskrive en lineær figur i rommet matematisk
- være i stand til å utlede vekstrate og doblingstid for eksponesiell bakterievekst
Nytt stoff
The data
Time, minutes | Number of E. coli bacteria |
---|---|
0 | 10010 |
30 | 9951 |
60 | 10042 |
90 | 25587 |
120 | 76327 |
150 | 212715 |
180 | 619511 |
210 | 1940838 |
240 | 4240760 |
Lists
t = [0, 30, 60, 90, 120, 150, 180, 210, 240]
E = [10010, 9951, 10042, 25587, 76327, 212715, 619511, 1940838, 4240760]
# [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] Indices
Linear functions
A straight line, or linear function, is written mathematically as: $$ \begin{equation} y = at + b, \label{_auto1} \end{equation} $$ where
- \( a \) is the slope (steepness) of the line
- \( b \) is where the curve intercepts the \( y \)-axis.
Linear functions
a
, the slope: $$ \begin{equation} a = \frac{\mathrm{change\; in \;}y}{\mathrm{change\; in \;}t}. \label{_auto2} \end{equation} $$
Linear functions
$$ \begin{equation} a = \frac{y_2-y_1}{t_2-t_1}. \label{_auto3} \end{equation} $$