y
.
In [1]:
import numpy as np
y = np.arctan(5)
math.ceil
and math.floor
functions do?They return round down to the nearest integer (ceil
) or up to the nearest integer (floor
).
y
, where $x$ is 2.
In [2]:
x = 2
y = 5*(x**4) - 3*x**2 + 0.5*x - 20
In [3]:
if x > -5 and x < 20:
print(x)
In [4]:
if x <= 5 and x >= 12:
print(x)
It will print c
. x
does not meet the first two conditionals (x > 2 or x < 0
and not x == 2
, so it will reach x == 2
. Since it equals 2, this will print c
and skip the last line printing d
.
help(range)
)
In [5]:
for i in range(1,2001,5):
print(i)
It will print 0 to 9
and then -10 to -99
.
In [6]:
# left hand integral
dx = 1
integral = 0
for x in range(-5,5):
integral = integral + dx*x**2
print(integral)
## A better, higher accuracy way
dx = 0.001
midpoints = np.arange(-5,5,dx) + dx/2
print(np.sum(midpoints**2)*dx)
In [7]:
some_list = []
for i in range(2,31):
some_list.append(i)
## another (better!) way is to cast it; faster
some_list = list(range(2,31))
sin(x)
for x
$\in$ [$0$, $\pi/4$, $\pi /2$, $3 \pi /4$... $2 \pi$].
In [8]:
import numpy as np
some_list = []
for i in range(0,9):
some_list.append(np.sin(np.pi*i/4))
print(some_list)
[100,-99,98,-97,96,...,0]
In [9]:
some_list = []
for i in range(-100,1):
if i % 2:
some_list.append(i)
else:
some_list.append(-i)
In [10]:
letters = "ABCDE"
some_dict = {}
for i in range(5):
some_dict[letters[i]] = i
## A different way using a cool function called enumerate
some_dict = {}
for number, letter in enumerate("ABCDE"):
some_dict[letter] = number
## Or even MORE compact using list comprehension
some_dict = dict([(letter,number) for number, letter in enumerate("ABCDE")])
Create a $3 \times 3$ numpy array with the integers 0-8:
[[0,1,2],
[3,4,5],
[6,7,8]]
Multiply the whole array by 5 and then take the natural log of all values (elementwise). What is the sum of the right-most column?
In [11]:
some_list = [[0,1,2],[3,4,5],[6,7,8]]
for i in range(3):
for j in range(3):
some_list[i][j] = some_list[i][j]*5
some_list[i][j] = np.log(some_list[i][j])
total = 0
for j in range(3):
total = total + some_list[j][2]
print(total)
sin(x)
for x
$\in$ [$0$, $\pi/4$, $\pi /2$, $3 \pi /4$... $2 \pi$].
In [12]:
some_array = np.array([[0,1,2],[3,4,5],[6,7,8]],dtype=np.int)
## OR
some_array = np.zeros((3,3),dtype=np.int)
total = 0
for i in range(3):
for j in range(3):
some_array[i,j] = total
total += 1
## OR (probably most efficient of the set)
some_array = np.array(range(9),dtype=np.int)
some_array = some_array.reshape((3,3))
print(np.sum(np.log((5*some_array))[:,2]))
np.log(some_array*5)
Out[12]:
In [13]:
def capitalize(some_string):
return some_string.upper()
capitalize("test")
Out[13]:
matplotlib
to plot $sin(x)$ for x
$\in$ [$0$, $\pi/4$, $\pi /2$, $3 \pi /4$... $2 \pi$]. Use both orange points and a green line.
In [14]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
x = np.arange(0,2.25*np.pi,0.25*np.pi)
y = np.sin(x)
plt.plot(x,y,"-",color="green")
plt.plot(x,y,"1",color="orange",markersize=12)
Out[14]:
You measure the doubling times of bacterial strains A-D under identical conditions.
strain | doubling time (min) |
---|---|
A | 20 |
B | 25 |
C | 39 |
D | 53 |
Assuming you start with a single cell and have nutrients in excess, you can calculate the number of bacteria $N(t)$ in a culture after $t$ minutes according to:
$$N(t) = 2^{t/d}$$
In [15]:
def num_bacteria(t,doubling_time):
return 2**(t/doubling_time)
In [16]:
doubling = {}
doubling["A"] = num_bacteria(12*60,20)
doubling["B"] = num_bacteria(12*60,25)
doubling["C"] = num_bacteria(12*60,39)
doubling
Out[16]:
matplotlib
to create a single graph that shows $N(t)$ for all four bacterial strains from 0 to 18 hr. Make sure you label your axes appropriately.
In [17]:
for k in doubling.keys():
print(k)
In [18]:
%matplotlib inline
from matplotlib import pyplot as plt
import numpy as np
t = np.arange(0,18*60+1,1)
some_dict = {"A":20.0,"B":25.0}
for k in some_dict.keys():
plt.plot(t,num_bacteria(t,some_dict[k]),".")
plt.yscale("log")
In [ ]: