02.lecture

_澳门葡京手机版app下载

BIOS1100 H17 uke 2

Lex Nederbragt

 

 

 


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)
  1. 13
  2. 4.0
  3. 31
  4. 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)
  1. 18.0
  2. 15.0
  3. 22.0
  4. 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?

  1. int(third)
  2. int(first)
  3. float(second)
  4. 1.0 * second

Answer. Option 3

Solution.

  1. Gives an error: one cannot use int() with a string that containing a number of type float
  2. int() converts to an integer, which does not have any digits
  3. Correct. even though the variable contains an integer (as a sting), the float() function will convert it to a float
  4. Since the second is a str this also results 'mixing int and str' 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?

  1. first + int(third)
  2. first + int(float(third))
  3. int(first) + int(float(third))
  4. 2.0 * second

Answer. Option 2

Solution. Note that the answer is a number of type float.

  1. This gives an error, as int cannot convert a text containing a number of type float
  2. float(third) converts "1.1" to a number of type float, and the int() function converts it to 1. So int(float(third)) results in an int. However, since first is a float, the final result will also be a float
  3. Both int(first) and int(float(third)) results in int's, so the final result will also be an int
  4. 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)
  1. ValueError
  2. TypeError
  3. SyntaxError
  4. NameError

Answer. Option 2. is correct.

Solution.


TypeError Traceback (most recent call last) in () 5 conc2 = (conc1*v1)/v2 6 ----> 7 print("The final concentration is: " + conc2)

TypeError: Can't convert 'float' object to str implicitly

Exercise 6: Rounding off

a) Description

my_number = 123.54

Which command will print 123?

  1. round(my_number,0)
  2. int(my_number)
  3. int(round(my_number,0))
  4. str(my_number)

Answer. The 2nd answer is correct.

Solution.

  1. The number will be rounded up, is still a float and this will print 124.0
  2. Correct. NOTE that the number is not rounded up!
  3. The number will first be rounded up and this will thus print 124
  4. 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))
  1. prints 1
  2. prints a
  3. gives SyntaxError
  4. 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} $$

 

Publisert 28. aug. 2017 17:08 - Sist endret 28. aug. 2017 17:08